{"id":248,"date":"2014-03-23T11:09:53","date_gmt":"2014-03-23T15:09:53","guid":{"rendered":"http:\/\/brian.digitalmaddox.com\/blog\/?p=248"},"modified":"2018-06-29T19:54:53","modified_gmt":"2018-06-29T23:54:53","slug":"using-free-geospatial-tools-and-data-part-10-usgs-gnis-data","status":"publish","type":"post","link":"https:\/\/brian.digitalmaddox.com\/blog\/?p=248","title":{"rendered":"Using Free Geospatial Tools and Data Part 10: USGS GNIS Data"},"content":{"rendered":"<p>The <a title=\"USGS Homepage\" href=\"http:\/\/www.usgs.gov\/\" target=\"_blank\">USGS<\/a> <a title=\"Board on Geographic Names\" href=\"http:\/\/geonames.usgs.gov\/\" target=\"_blank\">Board on Geographic Names<\/a> maintains the Geographic Names Information System (GNIS) database. \u00a0It is a database of over two million points in the United States. \u00a0This database:<\/p>\n<blockquote><p>contains information about physical and cultural geographic features in the United States and associated areas, both current and historical (not including roads and highways). The database holds the Federally recognized name of each feature and defines the location of the feature by state, county, USGS topographic map, and geographic coordinates.<\/p><\/blockquote>\n<p>You can download the 79 megabyte GNIS zip file from <a title=\"GNIS Download\" href=\"http:\/\/geonames.usgs.gov\/domestic\/download_data.htm\" target=\"_blank\">here<\/a>. \u00a0You will want to select the NationalFile as it is not broken up into individual states. \u00a0Importing GNIS into PostGIS is slightly more complicated as it does not come as a Shapefile, but instead as a 293 megabyte text file once it is unzipped from the \u00a0above. \u00a0Download the file, unzip it, and open a command window where the unzipped file is. \u00a0Note that the last time I did this on Windows, using the command line client was an exercise in pain due to how Windows handles code pages and character types. \u00a0On Windows it might be easier to do this inside something like <a title=\"pgadmin homepage\" href=\"http:\/\/www.pgadmin.org\/\" target=\"_blank\">pgadmin<\/a>.<\/p>\n<p>To import, first create a database inside PostgreSQL using something like the following:<\/p>\n<pre>createdb -T gistemplate USGS<\/pre>\n<p>Once done, you will want to run<\/p>\n<pre>psql -d USGS<\/pre>\n<p>to start the PostgreSQL database client. \u00a0Now you will want to create the table to hold the data. \u00a0To do this, copy and paste this statement into the <em>psql<\/em> client window:<\/p>\n<pre>CREATE TABLE gnis\r\n(\r\n feature_id integer NOT NULL,\r\n feature_name character varying,\r\n feature_class character varying,\r\n state_alpha character(2),\r\n state_numeric character(2),\r\n county_name character varying,\r\n county_numeric character(3),\r\n primary_lat_dms character varying,\r\n prim_long_dms character varying,\r\n prim_lat_dec real,\r\n prim_long_dec real,\r\n source_lat_dms character varying,\r\n source_long_dms character varying,\r\n source_lat_dec real,\r\n source_long_dec real,\r\n elev_in_m integer,\r\n elev_in_ft integer,\r\n map_name character varying,\r\n date_created date,\r\n date_edited date\r\n);<\/pre>\n<p>Note that I COULD have spent the time figuring out the maximum size of each column, instead of just making them varchars, to save space. \u00a0But, again, I&#8217;m lazy \ud83d\ude42<\/p>\n<p>Now to import, you will run the following command. \u00a0The .txt files is over two million rows, so it could take a while to import depending on the speed of your system.<\/p>\n<pre>\u00a0USGS=# \\<strong>copy gnis from NationalFile_20140204.txt DELIMITER '|' CSV HEADER<\/strong><\/pre>\n<p>If you get a file not found error,run <em>\\copy<\/em> with the full path to the NationalFile. \u00a0Depending on when you do this, the file name may be different based on when it was last updated.<\/p>\n<p>We are not done yet. \u00a0There is no actual geospatial geometry column in the database. \u00a0We will need to create one from the existing columns. \u00a0To do this, first we must create a geometry column to hold the geospatial points.<\/p>\n<pre>USGS=# <strong>SELECT AddGeometryColumn('public', 'gnis', 'geom', 4326, 'POINT', 2);<\/strong><\/pre>\n<p>This command tells PostgreSQL to add a geometry column named <em>geom<\/em> to the <em>gnis<\/em> table in the <em>public<\/em> schema using NAD83. \u00a0Now we need to actually populate this column. \u00a0We need to take the latitude and longitude columns in the table and convert them into a binary representation that PostGIS uses internally.<\/p>\n<pre>USGS=# <strong>update public.gnis <\/strong>\r\n<strong>SET geom = ST_PointFromText('POINT(' || prim_long_dec || ' ' || prim_lat_dec || ')', 4326);<\/strong><\/pre>\n<p>Here we have PostgreSQL convert the <em>prim_long_dec<\/em> and <em>prim_lat_dec<\/em> columns into a <a title=\"POINT Reference\" href=\"http:\/\/postgis.refractions.net\/documentation\/javadoc\/org\/postgis\/Point.html\" target=\"_blank\">POINT<\/a> and then to the actual geometry using the <a title=\"ST_PointFromText Function\" href=\"http:\/\/postgis.net\/docs\/manual-2.0\/ST_PointFromText.html\" target=\"_blank\">ST_PointFromText<\/a> function inside PostGIS.<\/p>\n<p>Now we need to add a geospatial index on the geom column. \u00a0You need an index to use the data in apps such as QGIS as it makes area look-ups much faster.<\/p>\n<pre>USGS=# <strong>create index gnis_geom_gist_idx on gnis using gist(geom);<\/strong><\/pre>\n<p>Now that we have an index, we need to create our database statistics and cluster it on the <em>geom<\/em> column. \u00a0As I mentioned in a previous post, you will run these commands in order (waiting for each one to complete before running the next):<\/p>\n<p>&nbsp;<\/p>\n<pre>USGS=# <strong>vacuum analyze gnis;<\/strong>\r\nUSGS=# <strong>cluster gnis using gnis_geom_gist_idx;\r\n<\/strong>USGS=# <strong>analyze gnis;<\/strong><\/pre>\n<p>And now we are done. \u00a0You have your own local copy of GNIS that you can use in visual GIS tools or from the command line. \u00a0There are some fun things you can do with the data, such as the below figure shows where I used QGIS to load all points in GNIS that have my last name in them (my modesty astounds even me \ud83d\ude09<\/p>\n<div id=\"attachment_251\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/brian.digitalmaddox.com\/blog\/wp-content\/uploads\/2014\/03\/maddoxplaces.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-251\" class=\"size-medium wp-image-251\" src=\"https:\/\/brian.digitalmaddox.com\/blog\/wp-content\/uploads\/2014\/03\/maddoxplaces-300x182.png\" alt=\"Maddox Places in QGIS\" width=\"300\" height=\"182\" srcset=\"https:\/\/brian.digitalmaddox.com\/blog\/wp-content\/uploads\/2014\/03\/maddoxplaces-300x182.png 300w, https:\/\/brian.digitalmaddox.com\/blog\/wp-content\/uploads\/2014\/03\/maddoxplaces-1024x621.png 1024w, https:\/\/brian.digitalmaddox.com\/blog\/wp-content\/uploads\/2014\/03\/maddoxplaces.png 1920w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-251\" class=\"wp-caption-text\">Maddox Places in QGIS<\/p><\/div>\n<p>Happy GISing!<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The USGS Board on Geographic Names maintains the Geographic Names Information System (GNIS) database. \u00a0It is a database of over two million points in the United States. \u00a0This database: contains information about physical and cultural geographic features in the United &hellip; <a href=\"https:\/\/brian.digitalmaddox.com\/blog\/?p=248\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-248","post","type-post","status-publish","format-standard","hentry","category-gis"],"_links":{"self":[{"href":"https:\/\/brian.digitalmaddox.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/brian.digitalmaddox.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brian.digitalmaddox.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brian.digitalmaddox.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/brian.digitalmaddox.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=248"}],"version-history":[{"count":7,"href":"https:\/\/brian.digitalmaddox.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/248\/revisions"}],"predecessor-version":[{"id":509,"href":"https:\/\/brian.digitalmaddox.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/248\/revisions\/509"}],"wp:attachment":[{"href":"https:\/\/brian.digitalmaddox.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brian.digitalmaddox.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brian.digitalmaddox.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}