{"id":100,"date":"2011-11-18T17:59:52","date_gmt":"2011-11-18T23:59:52","guid":{"rendered":"http:\/\/cartometric.com\/blog\/?p=100"},"modified":"2011-11-19T10:15:09","modified_gmt":"2011-11-19T16:15:09","slug":"ogr2ogr-export-well-known-text-wkt-for-one-feature-to-a-csv-file","status":"publish","type":"post","link":"https:\/\/elrobis.com\/blog\/2011\/11\/18\/ogr2ogr-export-well-known-text-wkt-for-one-feature-to-a-csv-file\/","title":{"rendered":"ogr2ogr: Export Well Known Text (WKT) for one feature to a CSV file"},"content":{"rendered":"<p>Perhaps you&#8217;re\u00a0looking for this?<\/p>\n<blockquote><p>ogr2ogr -f &#8220;CSV&#8221; &#8220;E:\\4_GIS\\NorthArkCartoData\\UnitedStates\\MO_wkt&#8221; &#8220;E:\\4_GIS\\NorthArkCartoData\\UnitedStates\\USStates.shp&#8221; -sql &#8221; SELECT * FROM usstates WHERE STATE_NAME = &#8216;Missouri&#8217; &#8221; -lco &#8220;GEOMETRY=AS_WKT &#8221; -lco &#8220;LINEFORMAT=CRLF&#8221; -lco &#8220;SEPARATOR=SEMICOLON&#8221;<\/p><\/blockquote>\n<p>My buddy at\u00a0work needed a way to get the WKT geometry definition for a single feature in a shapefile. I thought, &#8220;surely this is something we can do with OGR?&#8221; Lo&#8217; and behold, yes it was. \ud83d\ude42<\/p>\n<p>The script above\u00a0uses OGR\u00a0SQL to interrogate\u00a0a shapefile for one lone feature and, when it&#8217;s found, exports the record to a comma separated values (CSV) file (or in this case, a semicolon delimited file).\u00a0Here&#8217;s a quick break down:<\/p>\n<p><strong><em>-f &#8220;CSV&#8221; &#8220;E:\\4_GIS\\NorthArkCartoData\\UnitedStates\\MO_wkt&#8221;<\/em>\u00a0<\/strong> This means CREATE the directory <em>MO_wkt<\/em> and export\u00a0the derived output to this directory. I wrapped the file path in double-quotes (&#8220;) so linebreaks could not be introduced by the terminal and confuse the argument parser.<\/p>\n<blockquote><p>GOTCHA! DO NOT create the <em>MO_wkt<\/em> directory before running the script. Let OGR create that directory. However, relative to my example, <em>E:\\4_GIS\\NorthArkCartoData\\UnitedStates\\<\/em> could be on your file system already.<\/p><\/blockquote>\n<p><strong><em>&#8220;E:\\4_GIS\\NorthArkCartoData\\UnitedStates\\USStates.shp&#8221;\u00a0<\/em><\/strong> This is the shapefile you want to interrogate. Yet again, the file path is wrapped in double-quotes (&#8220;).<\/p>\n<p><strong><em>-sql &#8221;\u00a0 SELECT * FROM usstates WHERE STATE_NAME = &#8216;Missouri&#8217;\u00a0 &#8220;<\/em><\/strong>\u00a0\u00a0 This is the OGR SQL expression I used to grab the entire record (*) for features where the state_name field was &#8216;Missouri&#8217;. Notice how the search term (&#8216;Missouri&#8217;) is wrapped in single-quotes (&#8216;). If\u00a0the field you&#8217;re searching on is a\u00a0VARCHAR or a TEXT field\u00a0(obviously a name requires non-numeric characters), you&#8217;ll need to wrap your search term in single-quotes.\u00a0If you were searching on a numeric field, you would <em>not\u00a0<\/em>wrap your search term in\u00a0quotes.\u00a0Also, just like\u00a0the file paths shown above, I recommend <em>always<\/em> wrapping long values and expressions in double-quotes to avoid confusing the argument parser.\u00a0Next, and this is a matter of user-preference, but notice how I added extra spaces between the double-quotes and my SQL expression (i.e. &#8221;\u00a0 SELECT..\u00a0 &#8220;). This is\u00a0totally legit and it may help to\u00a0distinguish the SQL query from the rest of the OGR script.\u00a0<\/p>\n<p>I could have exported a subset of the fields using an expression like:<\/p>\n<p><em>&#8221;\u00a0 SELECT STATE_ABBR as abbr,\u00a0STATE_NAME as name FROM usstates WHERE STATE_NAME = &#8216;Missouri&#8217;\u00a0 &#8221; <\/em><\/p>\n<p>This expression selects only the <em>STATE_NAME<\/em> and the <em>STATE_ABBR<\/em> fields and renames them <em>name<\/em> and <em>abbr<\/em>, respectively, by applying the AS operator. The fields for the subset\u00a0are\u00a0comma-separated, but the last\u00a0field is <em>not<\/em> followed by a comma\u00a0(this is basic SQL syntax, but I&#8217;m explaining it in case the readership includes some first-timers).\u00a0If you don&#8217;t want\u00a0to rename the fields in your selection, just omit the &#8220;AS rename&#8221; from your expression. &#8212;when exporting to CSV, the geometry\u00a0will be\u00a0exported automatically according to the geometry flag used, which we set next. Unfortunately, though, this means there&#8217;s nothing you can do to omit the geometry from your selection.<\/p>\n<blockquote><p>These last few parameters use <strong>-lco<\/strong> flags\u00a0to\u00a0signalize various\u00a0&#8220;creation options&#8221; used by the CSV driver. The creation options\u00a0are unique for each dataset OGR can export, and in this case I&#8217;m demonstrating a particular recipie. If you want to know more about the\u00a0CSV creation options, check out the <a href=\"http:\/\/www.gdal.org\/ogr\/drv_csv.html\" target=\"_blank\">OGR CSV driver page<\/a>. Alternatively, if you want to export to a different file format, visit the <a href=\"http:\/\/www.gdal.org\/ogr\/ogr_formats.html\" target=\"_blank\">OGR formats page<\/a>, select the appropriate driver, and read-up on the various creation options.<\/p><\/blockquote>\n<p><strong><em>-lco &#8220;GEOMETRY=AS_WKT &#8220;<\/em><\/strong>\u00a0 This creation option means &#8220;Export the Geometry value\u00a0as Well Known Text&#8221;. Other options that might interest you include GEOMETRY=<strong>AS_XYZ<\/strong>, GEOMETRY=<strong>AS_XY<\/strong> or GEOMETRY=<strong>AS_YX<\/strong>.<\/p>\n<p><strong><em>-lco &#8220;LINEFORMAT=CRLF&#8221;\u00a0<\/em><\/strong> This creation option means &#8220;use a Windows-formatted linebreak&#8221;. If you&#8217;re running Linux you should use &#8220;LINEFORMAT=LF&#8221;. Honestly, I can&#8217;t remember if we really needed this flag\u00a0so you may want to try skipping this option.<\/p>\n<p><strong><em>-lco &#8220;SEPARATOR=SEMICOLON&#8221;\u00a0<\/em><\/strong> This flag specifies the delimiter used to separates fields and values from one another in the CSV. We chose SEMICOLON because Polygon WKT already has a bunch of commas in it, so using\u00a0the SEMICOLON delimiter helped us to visually identify the WKT we were really looking for. Your other options include SEPARATOR=COMMA and SEPARATOR=TAB.<\/p>\n<h2>Alternatively, do this\u00a0with ogrinfo..<\/h2>\n<p>You could also\u00a0use <a href=\"http:\/\/www.gdal.org\/ogrinfo.html\" target=\"_blank\">ogrinfo<\/a> to dump a feature&#8217;s WKT right into the console. This approach required\u00a0using one of the so-called <em>special fields<\/em> recognized by OGR SQL. To\u00a0learn\u00a0more about OGR SQL, its particulars, and\u00a0these special fields, visit <a href=\"http:\/\/www.gdal.org\/ogr\/ogr_sql.html\" target=\"_blank\">the OGR SQL help page<\/a>.\u00a0Without further adieu, here&#8217;s my ogrinfo script:<\/p>\n<blockquote><p>ogrinfo &#8220;E:\\4_GIS\\NorthArkCartoData\\UnitedStates\\USStates.shp&#8221; -geom=yes -sql &#8221; SELECT <strong>OGR_GEOM_WKT<\/strong> FROM usstates WHERE STATE_NAME = &#8216;Missouri&#8217; &#8220;<\/p><\/blockquote>\n<p>In this case, the OGR SQL expression requests only the &#8220;OGR_GEOM_WKT&#8221; <em>special field<\/em>. Because we&#8217;re using ogrinfo, our output will stay in the console. However, this\u00a0was ultimately undesirable for our task, as &#8220;real world&#8221; Polygon WKT typically spills beyond the console buffer. Moreover, copying the WKT geometry out of the console introduced hundreds of unwanted linebreaks into the WKT. So, for these reasons, we prefered to export our WKT results to a CSV because it allowed us to easily see, select, and copy the desired WKT data without any fuss using good ol&#8217; Notepad with word wrap.<\/p>\n<p>I hope you like. \ud83d\ude42<\/p>\n<p>\/Elijah<\/p>\n<p>P.S. If you don&#8217;t have OGR and want (<em>correction, you NEED<\/em>) to install it, check out my earlier post describing <a title=\"Install GDAL on Windows\" href=\"http:\/\/cartometric.com\/blog\/2011\/10\/17\/install-gdal-on-windows\/\" target=\"_blank\">how to install GDAL\/OGR on a Windows system<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Perhaps you&#8217;re\u00a0looking for this? ogr2ogr -f &#8220;CSV&#8221; &#8220;E:\\4_GIS\\NorthArkCartoData\\UnitedStates\\MO_wkt&#8221; &#8220;E:\\4_GIS\\NorthArkCartoData\\UnitedStates\\USStates.shp&#8221; -sql &#8221; SELECT * FROM usstates WHERE STATE_NAME = &#8216;Missouri&#8217; &#8221; -lco &#8220;GEOMETRY=AS_WKT &#8221; -lco &#8220;LINEFORMAT=CRLF&#8221; -lco &#8220;SEPARATOR=SEMICOLON&#8221; My buddy at\u00a0work needed a way to get the WKT geometry definition for a single feature in a shapefile. I thought, &#8220;surely this is something we can do [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[13],"tags":[15,8,14],"_links":{"self":[{"href":"https:\/\/elrobis.com\/blog\/wp-json\/wp\/v2\/posts\/100"}],"collection":[{"href":"https:\/\/elrobis.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/elrobis.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/elrobis.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/elrobis.com\/blog\/wp-json\/wp\/v2\/comments?post=100"}],"version-history":[{"count":12,"href":"https:\/\/elrobis.com\/blog\/wp-json\/wp\/v2\/posts\/100\/revisions"}],"predecessor-version":[{"id":104,"href":"https:\/\/elrobis.com\/blog\/wp-json\/wp\/v2\/posts\/100\/revisions\/104"}],"wp:attachment":[{"href":"https:\/\/elrobis.com\/blog\/wp-json\/wp\/v2\/media?parent=100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/elrobis.com\/blog\/wp-json\/wp\/v2\/categories?post=100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/elrobis.com\/blog\/wp-json\/wp\/v2\/tags?post=100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}