4

I am trying to import a shapefile (multipolygon) in an app with ogr but I got an error about invalid geom type. If I inspect the data with ogr like this:

datasource = ogr.Open("c:\\temp\\data.shp")
layer = datasource.GetLayer(0)
print(layer.GetGeomType())

The print returns:

-2147483645

I wonder what Geom Type is this. Is it an error in the data or is it a real Geom Type?

Edit: After using ogrinfo as elrobis suggested I have this output:

INFO: Open of C:\\ArcGIS data\\Mun_region.shp' using driverESRI Shapefile' successful.

Layer name: Mun_region Geometry: 3D Polygon Feature Count: 2 Layer SRS WKT: (unknown) OGR_GEOMETRY: String (0.0) OGRFeature(Mun_region):0
OGR_GEOMETRY (String) = MULTIPOLYGON

OGRFeature(Mun_region):1 OGR_GEOMETRY (String) = POLYGON

Below the Radar
  • 3,593
  • 1
  • 31
  • 58
  • 1
    What kind of app are you trying to import to? I can think of two options for you: 1) If you can import using ogr2ogr, you can set the -nlt flag to -nlt geometry, and ogr2ogr will attempt to insert both geom types. Alternatively, 2) you can just explode the multipart polygons to single part---realizing you'll probably need to involve some joining on a common field later---then run your import. – elrobis Apr 16 '13 at 20:05
  • 2
    ogr.wkbPolygon25D == -2147483645 – Mike T Apr 16 '13 at 21:23
  • Mike, ogr.wkbPolygon25D is 0x80000003, so it looks like there might be a failure to read as unsigned int in OGR's Python bindings. I was seeing the same thing in Fiona before this change: https://github.com/sgillies/Fiona/commit/b74b34507b14ad9c5a3bcfc8f887372405ef5843. – sgillies Apr 17 '13 at 18:22

1 Answers1

4

Ha. You may have an interesting problem here. Seeing that you have access to GDAL, and assuming you have access then to ogrinfo.. please adjust this expression and run it against your dataset to check for the distinct geometry types it contains:

Use OGRINFO to Return the Distinct Geometries in a Dataset

ogrinfo "D:\LongPathTo\Data\MyPolygons.shp" -geom=no -sql " SELECT DISTINCT OGR_GEOMETRY from MyPolygons "

Basically what I'm doing here is asking ogrinfo, by way of OGR SQL to show me a list of the distinct geometry types it encounters in my dataset. For my example, the output is like this:

C:\Documents and Settings\soloorbit>ogrinfo "D:\LongPathTo\Data\MyPolygons.shp" -geom=no -sql " SELECT DISTINCT
OGR_GEOMETRY from MyPolygons "
INFO: Open of `D:\LongPathTo\Data\MyPolygons.shp'
      using driver `ESRI Shapefile' successful.

Layer name: MyPolygons
Geometry: Polygon
Feature Count: 2
Layer SRS WKT:
(unknown)
OGR_GEOMETRY: String (0.0)
OGRFeature(MyPolygons):0
  OGR_GEOMETRY (String) = POLYGON

OGRFeature(MyPolygons):1
  OGR_GEOMETRY (String) = MULTIPOLYGON

So in this case---note that I have both POLYGON and MULTIPOLYGON types in this dataset. That's because ESRI allows that by default. But often, the open source stuff is more restrictive, and you generally have to say "hey this is okay!", or alternatively explode your multipart polys into single part.

....but more than anything, right now I'm just curious how many distinct geometries you have in that table. I wonder if it could be a null geometry?

CAVEAT: I've noticed the double-quote symbol (") has a tendency to get strange once it's posted into a web thread, so I highly-recommend just typing out the full expression into either notepad or your command window first, rather than merely copy/pasting it, then tweaking it.

Best / Elijah

elrobis
  • 6,456
  • 1
  • 31
  • 53
  • 1
    You might also want to look at an older GDAL_DEV thread that may explain what you've encountered. Here's the question, and here's the answer – elrobis Apr 16 '13 at 19:34
  • thank you, I didnt know about ogrinfo. Interesting tool. You were right I got two geometry type in the same shapefile. I have edited my post with the output – Below the Radar Apr 16 '13 at 20:01
  • Do you know how I could handle those cases? Does this geomtype is really wkbPolygon25D? – Below the Radar Apr 16 '13 at 20:02
  • maybe? :) ...when you say you're trying to import into an app, what exactly do you mean by app? Is it some instance of a database, or something very different? – elrobis Apr 16 '13 at 20:10
  • Its a geodjango app that loads shapefile into db using OGR. Cant use ogr2ogr for now because I use django ORM that needs defined models to work. But according to the answer you posted in first comment, I could handle this geomtype with wkbPolygon25D – Below the Radar Apr 16 '13 at 20:19
  • 1
    I confirm: ogr geom type is ogr.wkbPolygon25D for this shapefile. – Below the Radar Apr 16 '13 at 20:27
  • 2
    You can certainly try handling it as wkbPolygon25D, but I'm concerned that could be a red herring. @Burton449, First, if you have GDAL 1.8 or better, try using ogr2ogr just to create a new shapefile with all single part geometries, then see if you can import it using your normal approach. It's about like this: ogr2ogr "C:\ArcGIS data\Mun_region_SINGLEPART.shp" "C:\ArcGIS data\Mun_region.shp" -explodecollections – elrobis Apr 16 '13 at 20:37
  • Ahh... ok then, I'm glad you were able to make sense of it. Best / Elijah – elrobis Apr 16 '13 at 20:38
  • I did explodecollections with ogr2ogr and it works as expected. However, something else bother me. The geometry of this shapefile as Z value but its not suppose to. Is there a way to change that to 2d value? – Below the Radar Apr 16 '13 at 21:12
  • Hey @Burton449, I think that's actually better as a stand-alone question. I did some looking around and I'm not wholly sure how to answer you. For what it's worth, I suspect that once the file is commuted into your GeoDjango data model, the extra 25D a non-issue. (Just to make sure you know, 25D means, essentially, two-and-a-half dimensional, or that the data is packing additional z-values.) If multiple datasets for a project come from different sources/authorities, it would not surprise me to learn one party had 2D data and another had some inert 25D traits. – elrobis Apr 16 '13 at 22:03
  • Hello elrobis. I am not sure I want to support 25D in my app. Geodjango allow to use 3 dimension models with PostGIS to store this geometry, but I have not test this capability yet and dont know if it could affect performance or else. Anyway, thank you for your help, it was really appreciated. – Below the Radar Apr 17 '13 at 13:26