5

In my project I need to import some geometry from shapefiles.

Some of these are MULTIPOLYGON Z type, but all Z coordinates are 0-value.

When I try to save the geometry, I get the error:

"Geometry has Z dimension but column does not"

What is the best way to strip the Z dimension?

My code:

ds = DataSource(file_path, encoding='ISO-8859-1')
layers = ds[0]

#need something HERE to coerce geometry to 2D

obj=MyModel(geometry=GEOSGeometry(layers[0].geom.hex))
obj.save()
Jônatas Castro
  • 473
  • 3
  • 14

2 Answers2

8

Thanks for replying, Mike T.

The thing is that I need to make it using the GeoDjango framework, without directly accessing Postgis database.

Actually, after hard work, I found a solution. I need to use .clone() method from OGRGeometry object. Now, I could change coord_dim property. If I change coord_dim in the original object, nothing happens.

Here is my code:

ds = DataSource(file_path, encoding='ISO-8859-1')
layers = ds[0]

#HERE IS THE TRICK
new_layer = layers[0].geom.clone() 
new_layer.coord_dim = 2

obj=MyModel(geometry=GEOSGeometry(layers[0].geom.hex))
obj.save()
Jônatas Castro
  • 473
  • 3
  • 14
0

If you have SQL access, you can get this with ST_Force2D (or ST_Force_2D for older versions).

You can also correct the source table by modifying the type, e.g.:

ALTER TABLE my_table
  ALTER COLUMN geom TYPE geometry(MultiPolygon,4326) USING ST_Force2D(geom);
Mike T
  • 41,085
  • 18
  • 152
  • 203