8

I want to clip a shapefile to a shapefile. The input shapefile is a 1 m vector grid generated in QGIS. The tile I'm clipping to is just a simple polygon (the geometry is contained within the 1 m vector grid).

I put into the command line:

ogr2ogr -overwrite -t_srs EPSG:27700 -clipsrc ./Crewe_boundary_tile_grid_ID__0.shp ./Crewe_boundary_1m_grid_clipped_ID__0.shp ./Crewe_boundary_1m_grid.shp

However I get the error message:

ERROR 1: Attempt to write non-polygon (LINESTRING) geometry to POLYGON type shapefile. ERROR 1: Unable to write feature 500 from layer Crewe_boundary_1m_grid.

ERROR 1: Terminating translation prematurely after failed translation of layer Crewe_boundary_1m_grid (use -skipfailures to skip errors)

Yet if I try and run this for a different shapefile, i.e. a simple rectangular polygon, it works. I have also used exactly the same command on a 1000 m grid and that works. Also, if I do 'ogrinfo' then each of the input files reads as a polygon.

How do I fix this?

3
  • 1
    I guess that the error means that clip operation in ogr2ogr fails and creates a linestring instead of a polygon. Add -skipfailures and have a look at the resulting shapefile and consider if it is good enough. Alternatively write results into Spatialite as general geometries (-nlt GEOMETRY) and study with SQL what geometry types you have in the table. If you can isolate and repeat the error this could be a candidate for a GDAL bug report. If you can share the data files I can try them also.
    – user30184
    Jun 25, 2014 at 12:55
  • Thanks for your comment @user30184 - you can access the data here: dropbox.aber.ac.uk/?bXrgz7Xn
    – AMK
    Jun 25, 2014 at 13:04
  • I tried the same command with "-nlt Geometry" option but somehow the output after clipping has some features missing. Do i need to use any other option with -nlt ?
    – ashish
    Jul 4, 2019 at 11:52

1 Answer 1

9

The ogr2ogr clip operation creates polygons but for some reason also linestrings to to South and East edges of the area. Pink lines below show those 100 linestings (one is selected). Because of mixed geometrytypes the result cannot be saved into shapefile and therefore the error. I am not sure if this is an intended behaviour of ogr2ogr clipping or a bug. Anyway in your case it is nothing to be worried.

enter image description here

You can safely select to write a polygon shapefile and skip the errors. Using explicit -nlt is a good habbit or otherwise the new shapefile will take the geometrytype of the first feature that is met and that is not necessarily the one that is wanted.

ogr2ogr -f "ESRI shapefile" -clipsrc clip.shp output.shp input.shp -nlt POLYGON -skipfailures

Next time if you want to see and understand what happens you can make conversion into GML format which supports mixed geometries. Better way for my mind is to convert into Spatialite with a generic geometry type and do the rest of the research with SQL. The first steps could be

ogr2ogr -f "SQlite" -dsco spatialite=yes -clipsrc clip.shp output.sqlite input.shp -nlt GEOMETRY

create table lines as
select * from crewe_10m_grid
where geometrytype(geometry)='LINESTRING';

create table polygons as
select * from crewe_10m_grid
where geometrytype(geometry)='POLYGON';
7
  • Fantastic - thank you very much @user30184! I've no idea how I managed to get linestrings to the South and East? But thank you for giving me a solution!
    – AMK
    Jun 25, 2014 at 13:43
  • It's not you, GDAL made them for me too. It may be that lines are clipped from the next rows of polygons: clipping happens exactly at the two corners and thus to result is a line. If you are really interested, edit your clipping shp-file to be one centimeter bigger and you may get lean polygons instead of lines.
    – user30184
    Jun 25, 2014 at 14:14
  • Sorry, maybe I'm missing the point here, but how do I avoid creating linestrings? I've just created a new 1 m grid with integer coordinates, then created a series of polygons which share the same lines as the grid, then tried to use the clip function to clip my grid to the polygons and I still get the error: ERROR 1: Attempt to write non-polygon (LINESTRING) geometry to POLYGON type shapefile. So how do I avoid it? Sorry if this is an obvious question @user30184...
    – AMK
    Jun 26, 2014 at 8:42
  • You can't avoid it. Imagine a rectangle which is drawn with points A-B-C-D-A. The leftmost edge of the rectangle is A-D. If you clip this polygon with a line that goes via points A and D what is the result of the clip? It is a line A-D, isn't it? Result is correct and the only trouble you have is that you can't write linestrings into shapefile that is of polygon type. That you can avoid by adding -skipfailures into your ogr2ogr command.
    – user30184
    Jun 26, 2014 at 9:23
  • When I run it with -skipfailures and the other suggestions you made (i.e. ogr2ogr -f "ESRI shapefile" -clipsrc clip.shp output.shp input.shp -nlt POLYGON -skipfailures), it just gives me the errors and doesn't write the shapefile correctly - i.e. it's just a blank file.
    – AMK
    Jun 26, 2014 at 9:35

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.