Skip to content

Commit

Permalink
[OGR] Do not install spatial filter on a layer without geometry to av…
Browse files Browse the repository at this point in the history
…oid a OGR error to be triggered at msOGRFileNextShape() time (typical of a SQLite/Spatialite/GPKG SQL select layer that returns zero records) (#5317)
  • Loading branch information
rouault committed Sep 2, 2016
1 parent 2f547e1 commit 9b8a907
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 25 deletions.
52 changes: 27 additions & 25 deletions mapogr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2087,31 +2087,33 @@ static int msOGRFileWhichShapes(layerObj *layer, rectObj rect, msOGRFileInfo *ps

ACQUIRE_OGR_LOCK;

if (rect.minx == rect.maxx && rect.miny == rect.maxy) {
OGRGeometryH hSpatialFilterPoint = OGR_G_CreateGeometry( wkbPoint );

OGR_G_SetPoint_2D( hSpatialFilterPoint, 0, rect.minx, rect.miny );
OGR_L_SetSpatialFilter( psInfo->hLayer, hSpatialFilterPoint );
OGR_G_DestroyGeometry( hSpatialFilterPoint );
} else if (rect.minx == rect.maxx || rect.miny == rect.maxy) {
OGRGeometryH hSpatialFilterLine = OGR_G_CreateGeometry( wkbLineString );

OGR_G_AddPoint_2D( hSpatialFilterLine, rect.minx, rect.miny );
OGR_G_AddPoint_2D( hSpatialFilterLine, rect.maxx, rect.maxy );
OGR_L_SetSpatialFilter( psInfo->hLayer, hSpatialFilterLine );
OGR_G_DestroyGeometry( hSpatialFilterLine );
} else {
OGRGeometryH hSpatialFilterPolygon = OGR_G_CreateGeometry( wkbPolygon );
OGRGeometryH hRing = OGR_G_CreateGeometry( wkbLinearRing );

OGR_G_AddPoint_2D( hRing, rect.minx, rect.miny);
OGR_G_AddPoint_2D( hRing, rect.maxx, rect.miny);
OGR_G_AddPoint_2D( hRing, rect.maxx, rect.maxy);
OGR_G_AddPoint_2D( hRing, rect.minx, rect.maxy);
OGR_G_AddPoint_2D( hRing, rect.minx, rect.miny);
OGR_G_AddGeometryDirectly( hSpatialFilterPolygon, hRing );
OGR_L_SetSpatialFilter( psInfo->hLayer, hSpatialFilterPolygon );
OGR_G_DestroyGeometry( hSpatialFilterPolygon );
if( OGR_L_GetGeomType( psInfo->hLayer ) != wkbNone ) {
if (rect.minx == rect.maxx && rect.miny == rect.maxy) {
OGRGeometryH hSpatialFilterPoint = OGR_G_CreateGeometry( wkbPoint );

OGR_G_SetPoint_2D( hSpatialFilterPoint, 0, rect.minx, rect.miny );
OGR_L_SetSpatialFilter( psInfo->hLayer, hSpatialFilterPoint );
OGR_G_DestroyGeometry( hSpatialFilterPoint );
} else if (rect.minx == rect.maxx || rect.miny == rect.maxy) {
OGRGeometryH hSpatialFilterLine = OGR_G_CreateGeometry( wkbLineString );

OGR_G_AddPoint_2D( hSpatialFilterLine, rect.minx, rect.miny );
OGR_G_AddPoint_2D( hSpatialFilterLine, rect.maxx, rect.maxy );
OGR_L_SetSpatialFilter( psInfo->hLayer, hSpatialFilterLine );
OGR_G_DestroyGeometry( hSpatialFilterLine );
} else {
OGRGeometryH hSpatialFilterPolygon = OGR_G_CreateGeometry( wkbPolygon );
OGRGeometryH hRing = OGR_G_CreateGeometry( wkbLinearRing );

OGR_G_AddPoint_2D( hRing, rect.minx, rect.miny);
OGR_G_AddPoint_2D( hRing, rect.maxx, rect.miny);
OGR_G_AddPoint_2D( hRing, rect.maxx, rect.maxy);
OGR_G_AddPoint_2D( hRing, rect.minx, rect.maxy);
OGR_G_AddPoint_2D( hRing, rect.minx, rect.miny);
OGR_G_AddGeometryDirectly( hSpatialFilterPolygon, hRing );
OGR_L_SetSpatialFilter( psInfo->hLayer, hSpatialFilterPolygon );
OGR_G_DestroyGeometry( hSpatialFilterPolygon );
}
}

psInfo->rect = rect;
Expand Down
16 changes: 16 additions & 0 deletions msautotest/wxs/expected/wfs_ogr_native_sql_39.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Content-Type: text/xml; charset=UTF-8

<?xml version='1.0' encoding="UTF-8" ?>
<wfs:FeatureCollection
xmlns:ms="http://mapserver.gis.umn.edu/mapserver"
xmlns:wfs="http://www.opengis.net/wfs"
xmlns:gml="http://www.opengis.net/gml"
xmlns:ogc="http://www.opengis.net/ogc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd
http://mapserver.gis.umn.edu/mapserver http://localhost/path/to/wfs_simple?SERVICE=WFS&amp;VERSION=1.0.0&amp;REQUEST=DescribeFeatureType&amp;TYPENAME=select_zero_feature&amp;OUTPUTFORMAT=XMLSCHEMA">
<gml:boundedBy>
<gml:null>missing</gml:null>
</gml:boundedBy>
</wfs:FeatureCollection>

25 changes: 25 additions & 0 deletions msautotest/wxs/wfs_ogr_native_sql.map
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
# Test COUNT, STARTINDEX
# RUN_PARMS: wfs_ogr_native_sql_37.xml [MAPSERV] QUERY_STRING="map=[MAPFILE]&SERVICE=WFS&VERSION=2.0.0&REQUEST=GetFeature&TYPENAMES=towns&COUNT=1&STARTINDEX=2" > [RESULT_DEVERSION]
# RUN_PARMS: wfs_ogr_native_sql_38.xml [MAPSERV] QUERY_STRING="map=[MAPFILE]&SERVICE=WFS&VERSION=2.0.0&REQUEST=GetFeature&TYPENAMES=towns&COUNT=1&FILTER=<Filter><PropertyIsEqualTo><PropertyName>name</PropertyName><Literal>Granaglione</Literal></PropertyIsEqualTo></Filter>" > [RESULT_DEVERSION]
#
# Spatial query on a layer (unoptimized case with a 'SELECT ... FROM ... WHERE' DATA statement) that returns zero features
# RUN_PARMS: wfs_ogr_native_sql_39.xml [MAPSERV] QUERY_STRING="map=[MAPFILE]&SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature&TYPENAME=select_zero_feature&OUTPUTFORMAT=GML2&FILTER=<Filter><BBOX><PropertyName>Geometry</PropertyName><Box srsName='EPSG:32632'><coordinates>659159,4877386 696879,4898059</coordinates></Box></BBOX></Filter>" > [RESULT]

MAP

Expand Down Expand Up @@ -222,4 +225,26 @@ LAYER
TEMPLATE "wfs_ogr_native_sql.map"
END # Layer

LAYER
NAME select_zero_feature
DATA "SELECT * FROM towns WHERE 0"
CONNECTIONTYPE OGR
CONNECTION "./data/db.sqlite"
PROCESSING "NATIVE_SQL=YES"
METADATA
"ows_title" "select_zero_feature"
"wfs_featureid" "ID"
"gml_include_items" "all"
"gml_types" "auto"
"wfs_getfeature_formatlist" "ogrgml"
END
TYPE POINT
STATUS ON
PROJECTION
"init=epsg:32632"
END

TEMPLATE "wfs_ogr_native_sql.map"
END # Layer

END # Map File

0 comments on commit 9b8a907

Please sign in to comment.