Perhaps you’re looking for this?

ogr2ogr -f “CSV” “E:\4_GIS\NorthArkCartoData\UnitedStates\MO_wkt” “E:\4_GIS\NorthArkCartoData\UnitedStates\USStates.shp” -sql ” SELECT * FROM usstates WHERE STATE_NAME = ‘Missouri’ ” -lco “GEOMETRY=AS_WKT ” -lco “LINEFORMAT=CRLF” -lco “SEPARATOR=SEMICOLON”

My buddy at work needed a way to get the WKT geometry definition for a single feature in a shapefile. I thought, “surely this is something we can do with OGR?” Lo’ and behold, yes it was. 

The script above uses OGR SQL to interrogate a shapefile for one lone feature and, when it’s found, exports the record to a comma separated values (CSV) file (or in this case, a semicolon delimited file). Here’s a quick break down:

-f “CSV” “E:\4_GIS\NorthArkCartoData\UnitedStates\MO_wkt”  This means CREATE the directoryMO_wkt and export the derived output to this directory. I wrapped the file path in double-quotes (“) so linebreaks could not be introduced by the terminal and confuse the argument parser.

GOTCHA! DO NOT create the MO_wkt directory before running the script. Let OGR create that directory. However, relative to my example, E:\4_GIS\NorthArkCartoData\UnitedStates\ could be on your file system already.

“E:\4_GIS\NorthArkCartoData\UnitedStates\USStates.shp”  This is the shapefile you want to interrogate. Yet again, the file path is wrapped in double-quotes (“).

-sql “  SELECT * FROM usstates WHERE STATE_NAME = ‘Missouri’  “   This is the OGR SQL expression I used to grab the entire record (*) for features where the state_name field was ‘Missouri’. Notice how the search term (‘Missouri’) is wrapped in single-quotes (‘). If the field you’re searching on is a VARCHAR or a TEXT field (obviously a name requires non-numeric characters), you’ll need to wrap your search term in single-quotes. If you were searching on a numeric field, you would not wrap your search term in quotes. Also, just like the file paths shown above, I recommend always wrapping long values and expressions in double-quotes to avoid confusing the argument parser. Next, 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. “  SELECT..  “). This is totally legit and it may help to distinguish the SQL query from the rest of the OGR script.

I could have exported a subset of the fields using an expression like:

“  SELECT STATE_ABBR as abbr, STATE_NAME as name FROM usstates WHERE STATE_NAME = ‘Missouri’  ”

This expression selects only the STATE_NAME and the STATE_ABBR fields and renames them name and abbr, respectively, by applying the AS operator. The fields for the subset are comma-separated, but the last field isnot followed by a comma (this is basic SQL syntax, but I’m explaining it in case the readership includes some first-timers). If you don’t want to rename the fields in your selection, just omit the “AS rename” from your expression. —when exporting to CSV, the geometry will be exported automatically according to the geometry flag used, which we set next. Unfortunately, though, this means there’s nothing you can do to omit the geometry from your selection.

These last few parameters use -lco flags to signalize various ”creation options” used by the CSV driver. The creation options are unique for each dataset OGR can export, and in this case I’m demonstrating a particular recipie. If you want to know more about the CSV creation options, check out the OGR CSV driver page. Alternatively, if you want to export to a different file format, visit theOGR formats page, select the appropriate driver, and read-up on the various creation options.

-lco “GEOMETRY=AS_WKT “  This creation option means “Export the Geometry value as Well Known Text”. Other options that might interest you include GEOMETRY=AS_XYZ, GEOMETRY=AS_XY or GEOMETRY=AS_YX.

-lco “LINEFORMAT=CRLF”  This creation option means “use a Windows-formatted linebreak”. If you’re running Linux you should use “LINEFORMAT=LF”. Honestly, I can’t remember if we really needed this flag so you may want to try skipping this option.

-lco “SEPARATOR=SEMICOLON”  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 the SEMICOLON delimiter helped us to visually identify the WKT we were really looking for. Your other options include SEPARATOR=COMMA and SEPARATOR=TAB.

Alternatively, do this with ogrinfo..

You could also use ogrinfo to dump a feature’s WKT right into the console. This approach required using one of the so-called special fields recognized by OGR SQL. To learn more about OGR SQL, its particulars, and these special fields, visit the OGR SQL help page. Without further adieu, here’s my ogrinfo script:

ogrinfo “E:\4_GIS\NorthArkCartoData\UnitedStates\USStates.shp” -geom=yes -sql ” SELECTOGR_GEOM_WKT FROM usstates WHERE STATE_NAME = ‘Missouri’ “

In this case, the OGR SQL expression requests only the “OGR_GEOM_WKT” special field. Because we’re using ogrinfo, our output will stay in the console. However, this was ultimately undesirable for our task, as “real world” 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’ Notepad with word wrap.

I hope you like. 

/Elijah

P.S. If you don’t have OGR and want (correction, you NEED) to install it, check out my earlier post describinghow to install GDAL/OGR on a Windows system.

ogr2ogr: Export Well Known Text (WKT) for one feature to a CSV file的更多相关文章

  1. redmine export long csv file failed: 502 proxy error

    After modified the file \apps\redmine\conf\httpd-vhosts.conf: <VirtualHost *:8080> ServerName ...

  2. jQuery中的表单过滤选择器(四、七)::input、:text、:password、:radio、:checkbox、:file等

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  3. Casbin权限模型

    权限框架casbin1.概述Casbin是一个强大的.高效的开源访问控制框架,其权限管理机制支持多种访问控制模型. Casbin支持以下编程语言: Casbin可以做到:支持自定义请求的格式,默认的请 ...

  4. 如何理解Casbin的权限控制

    概念: Casbin是什么? Casbin是一个访问控制框架,可以支持多种访问控制模型(如ACL.RBAC.ABAC等) 目的: 我们最终想要实现的效果: 可以控制某一个人/角色(sub)能否对某个资 ...

  5. 进阶-案例九: WD中实现export 到Excel,Doc,Txt.

    1.导出excel 文件代码 导出事件代码: METHOD onactionimport . *导出excel: DATA: lo_node TYPE REF TO if_wd_context_nod ...

  6. csv impor export with mysql

    server-side:SELECT id,tutorialId,tutorialName,ucreatelink,structureVersion FROM base_courseINTO OUTF ...

  7. How to Export to Excel

    https://simpleisbetterthancomplex.com/tutorial/2016/07/29/how-to-export-to-excel.html Export data to ...

  8. PE Header and Export Table for Delphi

    Malware Analysis Tutorial 8: PE Header and Export Table 2. Background Information of PE HeaderAny bi ...

  9. WKT

    WKT - 概念 WKT(Well-known text)是一种文本标记语言,用于表示矢量几何对象.空间参照系统及空间参照系统之间的转换.它的二进制表示方式,亦即WKB(well-known bina ...

随机推荐

  1. HDU 2719 The Seven Percent Solution

    #include <cstdio> #include <cstring> int main() { ]; ]!='#') { ; while (i<strlen(s)) ...

  2. JAVA面试中的几个重要基础问题

    1.java是否会出现内存溢出?如何解决? 内存溢出是指应用系统中存在无法回收的内存或使用的内存过多,最终使得程序运行要用到的内存大于虚拟机能提供的最大内存.为了解决Java中内存溢出问题,我们首先必 ...

  3. 犯罪团伙利用POS机刷信用卡积分转卖 年获利千万

      今年1月20日,广东省公安厅展示去年缴获的盗刷专用POS机. 今年1月20日,广东省公安厅展示了一批缴获的盗刷信用卡工具. 他们是一群靠信用卡谋生的年轻人,平均年龄不超过30岁. 他们将各银行信用 ...

  4. Linux系统服务 1 ---- rSyslog日志服务

    1 日志 1 日志是系统用来记录系统运行时候的一些相关的信息的纯文本文件 2 日志的目的是保存相关程序的运行状态,错误信息等.为了对系统进行分析,保存历史记录以及在出现错误的时候发现分析错误使用 3 ...

  5. 自制的七个C,总结的太好了

    拿破仑·希尔把它叫做:“自制的七个C”: 1.控制自己的时间(Clock). 时间虽不断流逝,但也可以任人支配.你可以选择时间来工作.游戏.休息.烦恼..虽然客观的环境不一定能任人掌握,但人却可以自己 ...

  6. ecshop标签大全 各个页面常用标签大全

    先从index.php主页开始 页面关键字 {$keywords } 页面标题 {$page_title} 产品分类 父分类列表 {foreach from=$categories item=cat ...

  7. asp.net连接ORACLE数据库

    这段时间维护客户的一个系统,该系统使用的是ORACLE数据库,之前开发的时候用的都是MSSQL,并没有使用过ORACLE.这两种数据库虽然都是关系型数据库,但是具体的操作大有不同,这里作下记录. 连接 ...

  8. [C#参考]委托机制

    1. 委托概述 这是一个新的概念,但是其本质并不是什么新鲜的事物,委托本质上就是一个类.只不过一般的类是数据的集合,委托保存的是一个或者多个方法.委托是引用类型,因此委托有引用和对象,同时委托对象中包 ...

  9. 字节流复制mp3文件(带缓冲区)

    //自定义的缓冲区 import java.io.*; class  MyBufferedInputStream{    private byte[] buf = new byte[1024]; pr ...

  10. ThinkPHP第七天(F函数使用,项目分组配置,项目分组模板文件放置规则配置)

    1.F(文件名称,写入数据,写入地址),用于将数据写入至磁盘文件中,如F('Data',$arr,'./Data/'),作用是将$arr写入至网站根目录的Data文件夹中的Data.php中. 2.读 ...