Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GeoInterface to Python MapScript Classes #5765

Merged
merged 12 commits into from
Mar 25, 2019

Conversation

geographika
Copy link
Member

@geographika geographika commented Mar 13, 2019

This pull request adds a __geo_interface__ property for PointObj, LineObj, and ShapeObj.
See https://gist.github.com/sgillies/2217756 for details about the geo_interface.

It allows a GeoJSON-like interface for MapScript objects to help with interoperability with other geospatial Python libraries.

        res = layer.getResult(0)
        s = layer.getShape(res)
        s.itemdefinitions = l.getItemDefinitions()
        print(s.__geo_interface__)

Results in:

          {
            'geometry': {
                'type': 'Polygon',
                'coordinates': [[(-0.25, 51.227222), (-0.25, 51.727222), (0.25, 51.727222),
                                 (0.25, 51.227222), (-0.25, 51.227222)]]
            },
            'type': 'Feature',
            'bbox': (-0.25, 51.227222, 0.25, 51.727222),
            'properties': {'FNAME': 'A Polygon', 'FID': 1}
         }

A new MapScript method getItemDefinitions has been added to the layerObj.

Returns the requested item's field type.
A layer must be open to retrieve the item definition. 

Pass in the attribute index to retrieve the type. The 
layer's numitems property contains the number of items 
available, and the first item is index zero.

This allows the type of an item (field) to be returned as a string e.g. Character, Integer, Real (these are all GML data types as listed at https://mapserver.org/ogc/wfs_server.html).

Types are set on fields in a layer through gml_[item name]_type METADATA settings, or using "gml_types" "auto"

These types can then be set as a property on a shapeObj as shown in the example above so that item values are returned in the GeoJSON output as ints, floats, or strings.

A review of the C code in mapscript/swiginc/layer.i would be very much appreciated.

@geographika geographika changed the title Add GeoInterface to Python MapScript Classes [WIP] Add GeoInterface to Python MapScript Classes Mar 17, 2019
@geographika geographika changed the title [WIP] Add GeoInterface to Python MapScript Classes Add GeoInterface to Python MapScript Classes Mar 20, 2019
@geographika geographika requested a review from sdlime March 20, 2019 01:44
@sdlime
Copy link
Member

sdlime commented Mar 21, 2019

I'll review today...

@geographika
Copy link
Member Author

@sdlime - it is mainly the C function at https://github.com/mapserver/mapserver/pull/5765/files#diff-deee1b0c6f077b32fa05657f1799802c that it would be good to review. The Python extension code and tests should be fine. I changed the approach from returning a gmlItemDefinition which involved adding SWIG guards everywhere in the mapows.h and simply return the data type name as a copied string.

@sdlime
Copy link
Member

sdlime commented Mar 22, 2019

@sdlime - it is mainly the C function at https://github.com/mapserver/mapserver/pull/5765/files#diff-deee1b0c6f077b32fa05657f1799802c that it would be good to review. The Python extension code and tests should be fine. I changed the approach from returning a gmlItemDefinition which involved adding SWIG guards everywhere in the mapows.h and simply return the data type name as a copied string.

@geographika Sorry, sorry... Should check item_list is not NULL before accessing it since msGMLItems() can return if it has issues allocating memory. I think it's fine otherwise.

@geographika
Copy link
Member Author

@sdlime - thanks for the review. I've added in the check. I presume if item_list is NULL then there is no need to call msGMLFreeItems(item_list); and that line can stay in the If block?
If you're happy I can merge so it is included in the 7.4 release.

@sdlime
Copy link
Member

sdlime commented Mar 25, 2019

@sdlime - thanks for the review. I've added in the check. I presume if item_list is NULL then there is no need to call msGMLFreeItems(item_list); and that line can stay in the If block?
If you're happy I can merge so it is included in the 7.4 release.

Looks good to me - merge on!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants