[gdal-dev] Problems with OGRGeometry Intersects
Even Rouault
even.rouault at mines-paris.org
Tue Sep 4 14:50:37 PDT 2012
Le mardi 04 septembre 2012 23:04:16, Gabriel Fusca a écrit :
> Hi,
>
> I'm using Gdal 1.9.
>
> I have a function that has to perform a intersection of a geometry based on
> the click that is made on the screen.
>
> The raster file (Geotiff) that has a projected reference system (WGS 84 /
> UTM zone 21S) and the geometry a georeferenced system (EPSG - 4326 WGS84).
>
> The click that comes from the screen (spatial reference of the image)
> create a geometry from calling point.Buffer (radius).
>
> When i want to verify that the geometries intersect the intersects method
> from OGRGeometry fails, even if the querypoint is transformed into the
> spatial reference system of the geometry.
>
> Envelopes from the 2 geometries.
> QueryPoint
> env.
> if i generates the querypoint from the spatial reference of the geometry
> that i want to intersect)
> minx -61.66, maxx -53.66;
> miny -41.97, maxy -33.97
> else
> minx 443322.50, maxx 443330.50
> miny 5798153.59, maxy 5798161.59
>
> Geometry env.
> minx -57.79, maxx -57.59;
> miny -38.08, maxy-37.92
>
>
> The code is something like this:
>
> // spatialfilterwindow image subset transformed into
> // the spatial reference of the vector to intersect
> Extent spatialfilter(spatialfilterwindow);
> player->SetSpatialFilterRect(spatialfilter.min_.x_,
> spatialfilter.min_.y_,
> spatialfilter.max_.x_,
> spatialfilter.max_.y_);
> OGRLayer* player = ...;
> OGRFeature *pfeature = player->GetNextFeature();
> OGRPoint querypoint = OGRPoint(Point.x_, Point.y_);
> querypoint.assignSpatialReference(new OGRSpatialReference(SrWkt.c_str()));
> OGRGeometry* pquerypoly = querypoint.Buffer(radius);
> *while *(pfeature) {
> OGRGeometry *pgeometry = pfeature->GetGeometryRef();
> *if *((pgeometry && pgeometry->Intersects(pquerypoly) == TRUE)
>
> || (pquerypoly && pquerypoly->Intersects(pgeometry) == TRUE)) {
>
> // do something...
> } *else *{
> // handle the intersection fail
> }
> pfeature = player->GetNextFeature();
> }
>
> Any idea why it fails?
I suspect that you assumed assignSpatialReference() would do the reprojection,
which isn't the case (it just blindly assigns the specified SRS, but doesn't
alter the geometry coordinates). You must use the TransformTo() method
instead.
Incidental comment : you don't need to test ( A->Insersects(B) || B-
>Insersects(A)). Intersects() is a symetrical operation, so just one test will
be enough.
>
> Thanks,
> Regards,
More information about the gdal-dev
mailing list