Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a 3D shapefile in GDAL

I'm trying to create a 3D shapefile using GDAL. My code to create a 3D block looks like this:

OGRLayer *poLayer = ds->CreateLayer("Floor", NULL, wkbPolygon25D, NULL);
OGRFeature *poFeature;

poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );

OGRLinearRing ring1, ring2;
OGRPolygon poly;

double x0=y0=z0 = 0;
double x1=y1=z1 = 1;

ring1.addPoint(x0, y0, z0);
ring1.addPoint(x1, y0, z0);
ring1.addPoint(x1, y1, z0);
ring1.addPoint(x0, y1, z0);
ring1.addPoint(x0, y0, z0);
ring1.closeRings();
ring2.addPoint(x0, y0, z1);
ring2.addPoint(x1, y0, z1);
ring2.addPoint(x1, y1, z1);
ring2.addPoint(x0, y1, z1);
ring2.addPoint(x0, y0, z1);
ring2.closeRings();

poly.addRing(&ring1);
poly.addRing(&ring2);
poFeature->SetGeometry(&poly);

poLayer->CreateFeature(poFeature);

but, when I open the file later and query a point in the feature:

OGRGeometry* pGeometry = pFeature->GetGeometryRef();
OGRSpatialReference* pSpaRef = pGeometry->getSpatialReference();

OGRPoint point(0.5, 0.5,0.5);
point.assignSpatialReference(pSpaRef);    
OGRBoolean bContains = pGeometry->Contains(&point);

I get back a result of FALSE.

I've searched through the GDAL documentation and many other places and cannot find any example of how to build a 3D object, so I guess that if you created the top and bottom faces that it would connect them.

So, in summary, the question is...how do I create a 3D block object (like a cube) in GDAL?

like image 253
David Hope Avatar asked Jan 30 '26 19:01

David Hope


1 Answers

The problem is that you're making two rings - the top and bottom of a "box". This does not make a closed box (there are no sides), but rather 2 "plates" on top and bottom.

That being said, even if you do make this a closed box, contains will still fail. This does not do a volumetric contains check, but rather a check to see whether the point is contained within the actual surfaces of the box. Contains against a point defined as (0.5, 0.5, 1.0) should return true (provided OGR is compiled with GEOS), as that point is within the surfaces of the "box".

like image 151
Reed Copsey Avatar answered Feb 01 '26 08:02

Reed Copsey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!