|
1 | 1 | # Exercise 4: Create a polygon index map using virtual layer magic
|
2 |
| -- example: nationalmap NED1 |
3 |
| -- convert bounds from text format to polygons |
4 |
| -- QGIS virtual layers, SQL |
| 2 | + |
| 3 | +QGIS has many powerful tools for manipulating data. Virtual Layers are a way of generating new dynamic layers that are the result of using spatial SQL on existing layers. Virtual Layers do not need a spatial database like PostGIS or SpatiaLite, but can be used on any layer, including shapefiles or geojson. |
| 4 | + |
| 5 | +In this exercise, we will query the NationalMap to get a CSV of all the 1 arc-second DEM tiles for the entire United States, and then turn it into an index map. These tiles are 1-degree squares, and there are nearly 3400 of them. |
| 6 | + |
| 7 | + |
| 8 | +## 1. Download the tile info |
| 9 | + |
| 10 | +I'll demo this in the workshop, but the CSV has already been downloaded for you. But this same technique could be used to make index maps of other National Map datasets. |
| 11 | + |
| 12 | +- Go to https://viewer.nationalmap.gov/basic/ |
| 13 | +- On the map, zoom out so that all of US (including AK and HI) is visible |
| 14 | +- Under "Elevation Products (3DEP)", select "1 arc-second DEM" |
| 15 | +- Set the File Format to IMG |
| 16 | +- Click "Find Products" (wait for results to display) |
| 17 | +- Click the "Save as CSV" button |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +## 2. Create the tile index |
| 22 | + |
| 23 | +- Start a new QGIS project and add the /exercise4/ned3397_20200201_195900.csv file |
| 24 | + |
| 25 | +- Rename the layer to 'csv' (to make for simpler SQL below) |
| 26 | + |
| 27 | +If you open the attribute table, you'll see how the min/max x/y values are all jammed into a single field. This is not ideal, but we can extract each value using text-parsing functions in the SQL of QGIS Virtual Layers! |
| 28 | + |
| 29 | +- Open the Data Source Manager > Virtual Layer |
| 30 | + |
| 31 | +- Enter the following query: |
| 32 | +``` |
| 33 | +-- first we extract all the coordinates out of the boundingBox field |
| 34 | +with bbox as ( |
| 35 | + select |
| 36 | + *, |
| 37 | + regexp_substr("boundingBox", 'minX:(-?\d+(\.\d+)?)') as minX, |
| 38 | + regexp_substr("boundingBox", 'minY:(-?\d+(\.\d+)?)') as minY, |
| 39 | + regexp_substr("boundingBox", 'maxX:(-?\d+(\.\d+)?)') as maxX, |
| 40 | + regexp_substr("boundingBox", 'maxY:(-?\d+(\.\d+)?)') as maxY |
| 41 | + from csv |
| 42 | +) |
| 43 | +-- then we assemble the attribute table and polygon geometry |
| 44 | +select |
| 45 | + title, |
| 46 | + regexp_substr( "title", '(n\d+[ew]\d+)') as label, |
| 47 | + 'true' as available, |
| 48 | + downloadUrl AS downloadUrl, |
| 49 | + metaUrl AS websiteUrl, |
| 50 | + (prettyFileSize || ' - updated ' || publicationDate) as note, |
| 51 | + prettyFileSize AS size, |
| 52 | + lastUpdated AS updated, |
| 53 | + st_envelope( |
| 54 | + geom_from_wkt('LINESTRING('||minX||' '||minY||', '||maxX||' '||maxY||')') |
| 55 | + ) as geometry |
| 56 | +from bbox |
| 57 | +where downloadUrl not like '%rockyftp%' |
| 58 | +``` |
| 59 | + |
| 60 | +Note 1: "downloadURL AS downloadUrl" is necessary to get the capitalization correct for OpenIndexMaps. |
| 61 | +Note 2: We omit records for the old "rockyftp" server, which are for old duplicate tiles. |
| 62 | + |
| 63 | +- Under the "Geometry" section of the dialog, set the following: |
| 64 | + - Geometry column = geometry |
| 65 | + - Type = polygon |
| 66 | + - CRS = EPSG:4269 |
| 67 | + |
| 68 | +- Click "Add", then "Close" |
| 69 | + |
| 70 | +- Right-click the virtual layer > Export > Save Features As... |
| 71 | + Save to a geojson file "ned1-index.geojson" |
| 72 | + - Set CRS = EPSG:4326 (this theoretically isn't needed when RFC7946=YES, |
| 73 | + but will help avoid export errors) |
| 74 | + - Set Layer Options > RFC7946 = YES (to follow the newer geojson standard) |
5 | 75 |
|
0 commit comments