jazzband / geojson

Python bindings and utilities for GeoJSON
https://pypi.python.org/pypi/geojson/
BSD 3-Clause "New" or "Revised" License
898 stars 120 forks source link

Invalid GeoJSON type set on subclassing #164

Closed devleaks closed 2 years ago

devleaks commented 3 years ago

Hello,

When we create a subclass of a valid geojson class (ex. LineString), the subclass' type is not set to a valid GeoJSON type:

from geojson import Point, LineString

class Line(LineString):

    def __init__(self, start: Point, end: Point, width: float=None):
        LineString.__init__(self, ((start.coordinates, end.coordinates)))
        self.start = start
        self.end = end
        self.width = width

l = Line(start=Point((1.0, 1.0)), end=Point((1.0, 2.0)), width=1.0)
print(isinstance(l, LineString))
#True
print(l)
#{"coordinates": [[1.0, 1.0], [1.0, 2.0]], "end": {"coordinates": [1.0, 2.0], "type": "Point"}, "start": {"coordinates": [1.0, 1.0], "type": "Point"}, "type": "Line", "width": 1.0}

Please notice the "type": "Line" (which does not exist in GeoJSON) instead of "type": "LineString".

P.

rayrrr commented 2 years ago

Hi @devleaks what you are describing is expected and intended. The subclass you have created is no longer a valid GeoJSON type. There is no concept of "thickness" or "width" or "weight" of a LineString in GeoJSON. See IETF RFC7946 for more details.

devleaks commented 2 years ago

This is not a geojson issue. This is a python issue. Vanilla LineString generates valid geojson code. Please read my code. I derive a class from LineString, whatever the name and the reason. From that moment on, LineString no longer generate valid geojson code (in spite of the fact that the derived class is still an instanceOf LineString.) The issue is in the way you generate geojson inside LineString, way that is affected by subclassing and should not be, whatever the reason of the subclassing.