8
votes

I'm working with shape files in GeoDjango. Right now I'm trying to write a test for code that loads in a shape file and saves it to a database. The shape file currently has a feature count of 64,118. I'd like to reduce this to a handful so the test can quickly load it and confirm everything is right.

Since shape files aren't in a text format, is there a free application or library I can use to pluck out a handful of features and save them to a new file?

I should mention I don't have a license nor access to any of the ESRI product line.

1
Can these process work with shp files in a file geo-database? my files of aspect and slope have more than 16million records. best,GeorgeC
Perhaps these sort of questions would be also suitable on gis.stackexchange.com?Roman Luštrik

1 Answers

13
votes

You have several options to export a subset of records from a shapefile.

  • Any Open Source desktop GIS will be able to perform this. Some of the more populars are Quantum GIS, gvSIG or openJUMP. The exact steps will vary in each of them, but basically you have to load the shape file, start editing, select the records you want and export them to a new shapefile.

  • The ogr2ogr tool, part of the GDAL package allows you to transform between different geographic vector formats (or within the same format), and you can specify an SQL-like expression to filter the original dataset.

    ogr2ogr -f "ESRI Shapefile" -where "id < 10" new_shapefile.shp huge_shapefile.shp
    
  • If you are using PostGIS and don't want to install any of the previous apps, you can use the pgsql2shp tool to export a subset of your PostGIS table to a shapefile.

    pgsql2shp -f "/path/to/shapefile" -h server -u user -P password postgisdb 
     "SELECT * FROM table WHERE id < 10"
    

Edit: In any of the three options you can perform a spatial filter (ie features that fall within a bounding box) rather than a selection based on attributes.