Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strip Z dimension on GeoDjango (Force 2D geometry)

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()
like image 964
Jônatas Castro Avatar asked Mar 07 '16 18:03

Jônatas Castro


1 Answers

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()
like image 180
Jônatas Castro Avatar answered Sep 28 '22 13:09

Jônatas Castro