I downloaded GTOPO30 *.DEM files, and extracted a contour line at 2000m altitude using gdal_contour. In the resulting shapefile, I have many small polygons in areas where the altitude is close to that contour line, and the image looks quite noisy.

How can I, preferably using gdal/ogr or QGis, somehow 'downsample' these contours, so that the picture looks cleaner? I basically want to eliminate all small features.

PS: One way would be to downsample the grid before doing the contour extraction; is this the way to go, or should i rather downsample the shapefile?


Actually, I solved this using a combination of GDAL and QGis:

with GDAL:

Merge the different GTopo30 tiles into one file

gdal_merge.py -o gtopo30_usa.tif *.DEM

Down-sample this merged file to 0.1°

gdalwarp gtopo30_usa.tif -tr 0.1 0.1 gtopo30_usa_downsampled.tif

Extract the contour line

gdal_contour -fl 2000 gtopo30_usa_downsampled.tif gtopo30_usa_contour2000.shp

in QGis:

  • create new project
  • add vector layer, select the gtopo30_usa_contour2000.shp shapefile
  • Vector->Geometry->Lines to Polygons
  • Vector->Geometry->Add Geometry Columns
  • Layer->Query-> select an area threashold

When you say "small polygons" I assume they are actually LINESTRING features that form closed boundaries? THese are probably the contours encircling peaks that are just above 2000 m.

A possible solution is to import the shapefile into GRASS, and use the module v.edit tool=delete where="len< (some threshold) So the steps would be, after you have created a suitable GRASS Location and Mapset:

#Import the shapefile
v.in.ogr -e dsn=contours.shp out=contours_2000 type=line
# Create attrib table and add line lengths
v.db.addcol contours_2000 col="len double precision"
v.to.db contours_2000 option=length units=me col=len
# Now v.edit to delete all lines shorter than some threshold
# say 5000 meters
v.edit contours_2000 tool=delete where="len<5000"
# and export back out to shapefile
v.out.ogr contours_2000 dsn=contours_clean.shp


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.