Empty response is returned for GeoJSON when no features #5208

Merged
merged 1 commit into from Feb 23, 2016

Conversation

Projects
None yet
5 participants
@tbonfort
Member

tbonfort commented Feb 23, 2016

When using GeoJSON as output format with the OGR GeoJSON driver, an empty response (Content-Length: 0) is returned when no features match a query.

For GML an empty feature collection is returned, I would expect the same for GeoJSON. GeoServer does it this way and OpenLayers 3 also expects an empty feature collection.

@tbonfort

This comment has been minimized.

Show comment
Hide comment
@tbonfort

tbonfort Feb 23, 2016

Member

This is an OGR bug, mapserver itself does not create geojson. cc @rouault for confirmation

Member

tbonfort commented Feb 23, 2016

This is an OGR bug, mapserver itself does not create geojson. cc @rouault for confirmation

@tbonfort tbonfort closed this Feb 23, 2016

@tbonfort tbonfort added the Invalid label Feb 23, 2016

@dmorissette

This comment has been minimized.

Show comment
Hide comment
@dmorissette

dmorissette Feb 23, 2016

Contributor

Actually, @juliensam came across this issue as well with geojson and investigated a bit further. He will provide us more info on what a valid 0-result GeoJSON response should contain and we can make the fix in OGR (if the bug is indeed in OGR).

Contributor

dmorissette commented Feb 23, 2016

Actually, @juliensam came across this issue as well with geojson and investigated a bit further. He will provide us more info on what a valid 0-result GeoJSON response should contain and we can make the fix in OGR (if the bug is indeed in OGR).

@juliensam

This comment has been minimized.

Show comment
Hide comment
@juliensam

juliensam Feb 23, 2016

Contributor

According to the GeoJSON spec, a valid GeoJSON is a JSON document containing at least a geometry, a feature or an empty feature collection:
http://geojson.org/geojson-spec.html#geojson-objects

So according to the spec the minimal valid result for an empty response should be:

{
    "type": "FeatureCollection",
    "features": [
    ]
}

I'll let you decide if it's an OGR bug or a MapServer bug.

Here's a test mapfile with the test URL to reproduce the issue:

MAP

    # GetMap URL
    # &service=WMS&request=GetMap&version=1.3.0&CRS=EPSG:3857&width=200&height=200&layers=geojson&bbox=-20,-20,20,20&format=image/png

    # GetFeatureInfo URL (Working)
    # &service=WMS&request=GetFeatureInfo&version=1.3.0&CRS=EPSG:3857&width=200&height=200&layers=geojson&bbox=-20,-20,20,20&format=image/png&query_layers=geojson&i=100&j=100&&info_format=geojson
    IMAGETYPE png

    # GetFeatureInfo URL (Not Working)
    # &service=WMS&request=GetFeatureInfo&version=1.3.0&CRS=EPSG:3857&width=200&height=200&layers=geojson&bbox=-20,-20,20,20&format=image/png&query_layers=geojson&i=0&j=0&&info_format=geojson

    SIZE 100 100
    EXTENT -20 -20 20 20
    IMAGECOLOR '#ffffff'
    WEB
        METADATA
            "ows_enable_request" "*"
            "ows_srs" "EPSG:3857 EPSG:900913 EPSG:4326"
            "ows_title" "GeoJSON test"
            "wms_getfeatureinfo_formatlist" "gml,geojson"
            "wms_onlineresource" "http://localhost/cgi-bin/mapserv?map=mymap.map"
        END
        IMAGEPATH '/tmp/ms_tmp/'
        IMAGEURL '/ms_tmp/'
    END
    PROJECTION
        "init=epsg:3857"
    END

    OUTPUTFORMAT
      NAME "geojson"
      DRIVER "OGR/GEOJSON"
      MIMETYPE "application/json; subtype=geojson; charset=utf-8"
      FORMATOPTION "STORAGE=stream"
      FORMATOPTION "FORM=SIMPLE"
      FORMATOPTION "LCO:COORDINATE_PRECISION=5"
    END

    LAYER
        NAME "geojson"
        TYPE POLYGON
        STATUS DEFAULT
        PROJECTION
            'init=epsg:3857'
        END
        METADATA
            "wms_title" "Testing layer"
            "ows_srs" "EPSG:3857 EPSG:900913 EPSG:4326"
        END
        TEMPLATE 'blank.html'

        FEATURE
            POINTS
                -10 -10
                -10  10
                 10  10
                 10 -10
                -10 -10
            END
            TEXT "Polygon"
        END

        CLASS
            NAME 'class'

            STYLE
                COLOR '#ff0000'
            END
        END
    END

END

Contributor

juliensam commented Feb 23, 2016

According to the GeoJSON spec, a valid GeoJSON is a JSON document containing at least a geometry, a feature or an empty feature collection:
http://geojson.org/geojson-spec.html#geojson-objects

So according to the spec the minimal valid result for an empty response should be:

{
    "type": "FeatureCollection",
    "features": [
    ]
}

I'll let you decide if it's an OGR bug or a MapServer bug.

Here's a test mapfile with the test URL to reproduce the issue:

MAP

    # GetMap URL
    # &service=WMS&request=GetMap&version=1.3.0&CRS=EPSG:3857&width=200&height=200&layers=geojson&bbox=-20,-20,20,20&format=image/png

    # GetFeatureInfo URL (Working)
    # &service=WMS&request=GetFeatureInfo&version=1.3.0&CRS=EPSG:3857&width=200&height=200&layers=geojson&bbox=-20,-20,20,20&format=image/png&query_layers=geojson&i=100&j=100&&info_format=geojson
    IMAGETYPE png

    # GetFeatureInfo URL (Not Working)
    # &service=WMS&request=GetFeatureInfo&version=1.3.0&CRS=EPSG:3857&width=200&height=200&layers=geojson&bbox=-20,-20,20,20&format=image/png&query_layers=geojson&i=0&j=0&&info_format=geojson

    SIZE 100 100
    EXTENT -20 -20 20 20
    IMAGECOLOR '#ffffff'
    WEB
        METADATA
            "ows_enable_request" "*"
            "ows_srs" "EPSG:3857 EPSG:900913 EPSG:4326"
            "ows_title" "GeoJSON test"
            "wms_getfeatureinfo_formatlist" "gml,geojson"
            "wms_onlineresource" "http://localhost/cgi-bin/mapserv?map=mymap.map"
        END
        IMAGEPATH '/tmp/ms_tmp/'
        IMAGEURL '/ms_tmp/'
    END
    PROJECTION
        "init=epsg:3857"
    END

    OUTPUTFORMAT
      NAME "geojson"
      DRIVER "OGR/GEOJSON"
      MIMETYPE "application/json; subtype=geojson; charset=utf-8"
      FORMATOPTION "STORAGE=stream"
      FORMATOPTION "FORM=SIMPLE"
      FORMATOPTION "LCO:COORDINATE_PRECISION=5"
    END

    LAYER
        NAME "geojson"
        TYPE POLYGON
        STATUS DEFAULT
        PROJECTION
            'init=epsg:3857'
        END
        METADATA
            "wms_title" "Testing layer"
            "ows_srs" "EPSG:3857 EPSG:900913 EPSG:4326"
        END
        TEMPLATE 'blank.html'

        FEATURE
            POINTS
                -10 -10
                -10  10
                 10  10
                 10 -10
                -10 -10
            END
            TEXT "Polygon"
        END

        CLASS
            NAME 'class'

            STYLE
                COLOR '#ff0000'
            END
        END
    END

END

@juliensam juliensam reopened this Feb 23, 2016

@tbonfort tbonfort added this to the 7.0.1 milestone Feb 23, 2016

@tbonfort tbonfort removed the Invalid label Feb 23, 2016

@tbonfort

This comment has been minimized.

Show comment
Hide comment
@tbonfort

tbonfort Feb 23, 2016

Member

The proposed patch fixes the issue, it now emits the layer even if the query returned no features. @sdlime: ok?

Member

tbonfort commented Feb 23, 2016

The proposed patch fixes the issue, it now emits the layer even if the query returned no features. @sdlime: ok?

@sdlime

This comment has been minimized.

Show comment
Hide comment
@sdlime

sdlime Feb 23, 2016

Member

Looks ok to me... --Steve

Member

sdlime commented Feb 23, 2016

Looks ok to me... --Steve

@juliensam

This comment has been minimized.

Show comment
Hide comment
@juliensam

juliensam Feb 23, 2016

Contributor

With the test mapfile above, I get the following response:

{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3857" } },
"features": [

]
}

Merging

Contributor

juliensam commented Feb 23, 2016

With the test mapfile above, I get the following response:

{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3857" } },
"features": [

]
}

Merging

juliensam added a commit that referenced this pull request Feb 23, 2016

Merge pull request #5208 from tbonfort/issues/5208-empty-ogr-output
OGR output: emit layers with no results

@juliensam juliensam merged commit 2e8c128 into mapserver:branch-7-0 Feb 23, 2016

2 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@rouault

This comment has been minimized.

Show comment
Hide comment
@rouault

rouault Feb 24, 2016

Contributor

I hope all drivers generate valid files with 0 feature written in the layer... I guess most should behave OK.

Contributor

rouault commented Feb 24, 2016

I hope all drivers generate valid files with 0 feature written in the layer... I guess most should behave OK.

@sdlime

This comment has been minimized.

Show comment
Hide comment
@sdlime

sdlime Feb 24, 2016

Member

All OGR drivers, correct?

Member

sdlime commented Feb 24, 2016

All OGR drivers, correct?

@rouault

This comment has been minimized.

Show comment
Hide comment
@rouault

rouault Feb 24, 2016

Contributor

Yes OGR drivers. But anyway the change should be OK.

Contributor

rouault commented Feb 24, 2016

Yes OGR drivers. But anyway the change should be OK.

tbonfort added a commit to mapserver/msautotest_DEPRECATED that referenced this pull request Feb 24, 2016

@tbonfort

This comment has been minimized.

Show comment
Hide comment
@tbonfort

tbonfort Feb 24, 2016

Member

autotests added. @juliensam please don't merge pull-requests before discussions concerning the implications have happened.

Member

tbonfort commented Feb 24, 2016

autotests added. @juliensam please don't merge pull-requests before discussions concerning the implications have happened.

@juliensam

This comment has been minimized.

Show comment
Hide comment
@juliensam

juliensam Feb 24, 2016

Contributor

sorry about this. Noted for the future.

Contributor

juliensam commented Feb 24, 2016

sorry about this. Noted for the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment