OGR vector data tips and tricks

[url=http://gfoss.blogspot.com/2008/06/ogr-vector-data-tips-and-tricks.html]OGR vector data tips and tricks[/url]
by markusN

[size=medium][b]Get vector map extent[/b][/size]
You can easily grep the map extent of a vector map (bounding box):
ogrinfo /cdrom/ITALY_GC.SHP ITALY_GC | grep Extent
Extent: (9.299460, 43.787362) - (13.911350, 47.070188)

[size=medium][b]Merge of two SHAPE files[/b][/size]
Merge of two SHAPE files 'file1.shp' and 'file2.shp' into a new file 'file_merged.shp' is performed like this:
ogr2ogr file_merged.shp file1.shp
ogr2ogr -update -append file_merged.shp file2.shp -nln file_merged file2

The second command is opening file_merged.shp in update mode, and trying to find existing layers and append the features being copied. The -nln option sets the name of the layer to be copied to.

[size=medium][b]Vector map reprojection[/b][/size]
We reproject from the source projection (as defined in .prj file) to WGS84/LL:
ogr2ogr vmap0rd_ll.shp -t_srs "EPSG:4326" vmap0rd.shp

If the .prj file is missing, you can use the 'epsg_tr.py' utility to create it if you know the EPSG code:
epsg_tr.py -wkt 4326 > cities.prj

[size=medium][b]Reproject to current GRASS location projection[/b][/size]:
ogr2ogr -t_srs "`g.proj -wf`" polbnda_italy_GB_ovest.shp polbnda_italy_LL.shp

[size=medium][b]Cut out a piece of a vector map[/b][/size]
Use spatial query extents: -spat xmin ymin xmax ymax (W S E N)
ogr2ogr ARC_BZ.shp -spat 10 45 13 47 ARC.shp

[size=medium][b]Get VMAP0 metadata info[/b][/size]
ogrinfo -ro gltp:/vrf/grass0/warmerdam/v0soa/vmaplv0/soamafr
ogrinfo -ro gltp:/vrf/grass0/warmerdam/v0soa/vmaplv0/soamafr | grep bnd
ogrinfo -ro gltp:/vrf/grass0/warmerdam/v0soa/vmaplv0/soamafr 'polbnda@bnd(*)_area'
ogrinfo -ro gltp:/vrf/grass0/warmerdam/v0eur/vmaplv0/eurnasia 'roadl@trans(*)_line'

[size=medium][b]MAP0: Extract spatial subregion, reproject from NAD83 to WGS84[/b][/size]
# coordinate order: W S E N
ogr2ogr -spat 19.95035 -26.94755 29.42989 -17.72624 -t_srs 'EPSG:4326' \
polbnda_botswana.shp gltp:/vrf/grass0/warmerdam/v0soa/vmaplv0/soamafr \
'polbnda@bnd(*)_area'

[size=medium][b]OGR and SQ[/b]L[/size]
Sample 'where' statements (use -sql for PostgreSQL driver):
# -where 'fac_id in (195,196)'
# -where 'fac_id = 195'
ogrinfo -ro -where 'fac_id in (195,196)' \
gltp:/vrf/grass0/warmerdam/v0soa/vmaplv0/soamafr 'polbnda@bnd(*)_area'

[size=medium][b]Find out the Countries VMAP0 coding[/b][/size]
ogdi_info -u gltp:/vrf/grass0/warmerdam/v0soa/vmaplv0/soamafr \
-l 'polbnda@bnd(*)_area' -f area | grep Botswana
or read the VMAP0 Military specs, page 75pp

[size=medium][b]Extract Botswana, reproject on the fly from NAD83 to WGS84, store as SHAPE:[/b][/size]
ogr2ogr -t_srs "EPSG:4326" -where "na2 = 'BC'" polbnda_botswana.shp \
gltp:/vrf/grass0/warmerdam/v0soa/vmaplv0/soamafr 'polbnda@bnd(*)_area'

[size=medium][b]Extract Germany, reproject on the fly from NAD83 to WGS84, store as SHAPE:[/b][/size]
ogr2ogr -t_srs "EPSG:4326" -where "na2 = 'GM'" polbnda_germany.shp
gltp:/vrf/grass0/warmerdam/v0eur/vmaplv0/eurnasia 'polbnda@bnd(*)_area'
ogrinfo -summary polbnda_germany.shp polbnda_germany | grep Extent
# Extent: (5.865639, 47.275776) - (15.039889, 55.055637)
# W S E N

[size=medium][b]VMAP0 Contour lines for Germany:[/b][/size]
ogr2ogr -t_srs "EPSG:4326" -spat 5.865639 47.275776 15.039889 55.055637 \
contour_lines.shp \
gltp:/vrf/grass0/warmerdam/v0eur/vmaplv0/eurnasia 'contourl@elev(*)_line'

[size=medium][b]VMAP0 elevation spots (points) for Germany:[/b][/size]
ogr2ogr -t_srs "EPSG:4326" -spat 5.865639 47.275776 15.039889 55.055637 \
elevation_spots.shp \
gltp:/vrf/grass0/warmerdam/v0eur/vmaplv0/eurnasia 'elevp@elev(*)_point'

[size=medium][b]VMAP0 lakes of Trentino province in Italy[/b][/size]
ogr2ogr -t_srs "EPSG:4326" -where "na2 = 'IT'" \
-spat 10.340029 45.261888 10.98727 45.98993 \
lakes_italy.shp \
gltp:/vrf/grass0/warmerdam/v0eur/vmaplv0/eurnasia 'inwatera@hydro(*)_area'

[size=medium][b]Connect OGR and PostgreSQL/PostGIS[/b][/size]
ogrinfo PG:'host=grass.itc.it user=postgres dbname=ogc_simple'
ogr2ogr out.shape PG:'host=grass.itc.it user=postgres dbname=ogc_simple' lake_geom

[size=medium][b]GRASS 6 and OGR[/b][/size]
Convert GRASS 6 vector map to SHAPE (needs GDAL-OGR-GRASS plugin):
# -nln is "new layer name" for the result:
ogr2ogr archsites.shp grassdata/spearfish60/PERMANENT/vector/archsites/head 1 \
-nln archsites

[size=medium][b]Using WKT files with ogr2ogr[/b][/size]
The definition is in ESRI WKT format. If you save it to a text file called out.wkt you can do the following in a translation to reproject input latlong points to this coordinate system:
ogr2ogr -s_srs WGS84 -t_srs ESRI::out.wkt out_dir indatasource

Most comand line options for GDAL/OGR tools that accept a coordinate system will allow you to give the name of a file containing WKT. And if you prefix the filename with ESRI:: the library will interprete the WKT as being ESRI WKT and convert to "standard" format accordingly. The -s_srsswitch is assigning a source coordinate system to your input data (in case it didn't have this properly defined already), and the -t_srs is defining a target coordinate system to reproject to.

[size=medium][b]TIGER files in OGR[/b][/size]
# linear features:
ogr2ogr tiger_lines.shp tgr46081.rt1 CompleteChain

# area features:
export PYTHONPATH=/usr/local/lib/python2.5/site-packages
tigerpoly.py tgr46081.rt1 tiger_area.shp

[size=medium][b]OGR CSV driver: easily indicate column types[/b][/size]
You can now write a little csv help file to indicate the columns types to OGR. It works as follow :
Suppose you have a foobar.csv file that looks like this:
"ID","X","Y","AREA","NAME"
"1","1023.5","243.56","675","FOOBAR"
...

Now write a foobar.csvt file like this one:
"Integer","Real","Real","Integer","String"
The driver will then use the types you specified for the csv columns. The types recognized are Integer, Real and String, DateTime, and Date.

[size=medium][b]Convert KML to CSV (WKT)[/b][/size]
First find layers:
ogrinfo -so myfile.kml

Then convert KML to CSV:
ogr2ogr -f CSV out.csv myfile.kml -sql "select *,OGR_GEOM_WKT from myfilelayer"
cat out.csv


Or use the cool online converter: http://geoconverter.hsr.ch
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值