I am running a simple script to clip and convert all tab files in a directory via osgeo shell

for %f in (*.TAB) do ogr2ogr "%~dpnf_Clip.shp" -clipsrc "clipped\AOI.TAB" "%f" -f "ESRI Shapefile"

It mostly works but for some files I get

ERROR 1: Attempt to write non-point (LINESTRING) geometry to point shapefile. ERROR 1: Unable to write feature 602 from layer Water_Notes_DSC. ERROR 1: Terminating translation prematurely after failed

How can I get it to split these features into _point, _line and _poly automatically?

  • 1
    there's a little option in the help that isn't explained gdal.org/ogr2ogr.html [layer[layer..]] it seems that you can specify Point, Line (polyline) or Polygon layer from your TAB, which supports multiple geometry types in the same file, into a shapefile which supports only one geometry type. – Michael Stimson May 29 '18 at 6:14
  • 1
    There is only one layer in that tab so that won't work. – user30184 May 29 '18 at 6:46
  • 1
    If you use orginfo your_tab_file, you may get more info, the features are grouped by geometry type and have an ordinal number (org calls it layer). – Zoltan May 29 '18 at 7:23

MapInfo tab can contain several layers but obviously in this case there is just one layer that holds several kind of geometries. Like this one that I made for testing:

ogrinfo mixed.tab mixed
INFO: Open of `mixed.tab'
      using driver `MapInfo File' successful.

Layer name: mixed
Geometry: Unknown (any)
Feature Count: 3
  FID (Integer) = 1
  POINT (260000.01 7240000.01)

  FID (Integer) = 2
  LINESTRING (159999.99 7039999.99,300000 7140000,189999.99 7210000.01)

  FID (Integer) = 3
  POLYGON ((-30000.0 7240000.01,39999.99 7159999.99,99999.99 7279999.99,-30000.0 7240000.01))

I suggest to use ogr2ogr with the -sql parameter. As documented in http://www.gdal.org/ogr_sql.html you can select features by the geometry type.

ogr2ogr -f "ESRI Shapefile" points.shp mixed.tab -sql "SELECT* FROM mixed where ogr_geometry='POINT' OR ogr_geometry='MULTIPOINT'"

ogr2ogr -f "ESRI Shapefile" lines.shp mixed.tab -sql "SELECT* FROM mixed where ogr_geometry='LINESTRING' OR ogr_geometry='MULTILINESTRING'"

ogr2ogr -f "ESRI Shapefile" polygons.shp mixed.tab -sql "SELECT* FROM mixed where ogr_geometry='POLYGON' OR ogr_geometry='MULTIPOLYGON'"
| improve this answer | |
  • this, or use the -where "<filter_expression" flag or the -nlt <geometry_type> flag – geozelot May 29 '18 at 14:01
  • 1
    Layer type (-nlt) may give unexpected result. It converts polygon also to line layer as a linear ring. – user30184 May 29 '18 at 14:15
  • @user30184 great suggestion but the problem is that it's being run on a whole directory of files where I don't know if the tab being processed has more than one kind of geometry. Apart from creating a python process that does an ogrinfo and if there's more than one type, it uses your code example with -sql to split an input file into outfile_line, outfile_poly, outfile_point -can you think of a way to do this within a simple one-liner? – GeorgeC May 29 '18 at 23:35
  • 1
    Brute force is to run all three commands. It is not necessarily so slow. – user30184 May 30 '18 at 10:21

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.