This site contains excerpts and examples of topics covered in the book. Available Now at Pragmatic Bookshelf!!
[View Contents] [Forum]
Suppose you want to extract features from a vector layer to create a smaller subset. This can be very handy if you have a large dataset and need just a portion of it for your current project. If your data happens to be in one of the formats supported by OGR, you can use ogr2ogr to create a new layer.
First let's look at extracting by a simple bounding box. Say we want to pull Canada, United States, and Mexico from the world_borders shapefile. This can easily be done with a small bounding box that crosses each of the countries:
ogr2ogr -spat -111 20 -108 56 northamerica.shp world_borders.shp
The result is shown below, with the new shapefile (northamerica.shp) shown in green. For reference, the bounding box used to extract the features is shown in red/orange.

The second way to extract features is by attribute. In this case, we use a simple where clause with ogr2ogr to specify which countries we want in the new layer:
ogr2ogr -where "CNTRY_NAME='Canada' or CNTRY_NAME='Mexico' or CNTRY_NAME='United States'" \ northamerica2.shp world_borders.shp

As you can see, ogr2ogr provides two quick ways to subset your data and create new layers for any OGR supported format. See the documentation for ogr2ogr for information on specifying different output formats and options.
Recent comments
2 weeks 3 days ago
2 weeks 3 days ago
2 weeks 6 days ago
18 weeks 2 days ago
35 weeks 4 days ago
36 weeks 3 days ago
42 weeks 3 days ago
1 year 16 weeks ago
1 year 17 weeks ago
1 year 26 weeks ago