Пример #1
0
 def default(self, obj):
     if isinstance(obj, (datetime.date, datetime.datetime, datetime.time)):
         return obj.isoformat()
     if isinstance(obj, decimal.Decimal):
         # The decimal is converted to a lossy float
         return float(obj)
     return GeoJSONEncoder.default(self, obj)
Пример #2
0
    def default(self, obj):
        geom_type = None
        if hasattr(obj, '__geo_interface__'):
            geom_type = dict(obj.__geo_interface__)['type']

        if isinstance(obj, (decimal.Decimal, datetime.date, datetime.datetime)):
            return str(obj)

        if isinstance(obj, GeoInterface):
            self.srs = obj.geometry_column().type.srid
            obj = obj.__geo_interface__

        if isinstance(obj, (geojson.GeoJSON)):
            ret = dict(obj)
            if 'coordinates' in ret.keys():
                coordinates = ret['coordinates']

            if isinstance(obj, (geojson.Feature, geojson.feature.Feature)) or geom_type == 'Feature':
                ret = dict(obj)
                geometry = self.default(obj.geometry)
                ret = dict(obj)
                ret['geometry'] = geometry
                esriType = 'esriGeometryPoint'
                if 'rings' in geometry.keys():
                    esriType = 'esriGeometryPolygon'
                if 'paths' in geometry.keys():
                    esriType = 'esriGeometryPolyline'
                ret['geometryType'] = esriType

                return self._cleanup(ret)

            if isinstance(obj, (geojson.FeatureCollection)):
                features = []
                for feature in obj.features:
                    features.append(self.default(feature))

                ret = dict(obj)
                ret['features'] = features
                return self._cleanup(ret)

            if isinstance(obj, (geojson.Point)):
                ret = dict(obj)

                ret['x'], ret['y'] = coordinates
                ret['type'] = 'esriGeometryPoint'

                return self._cleanup(ret)

            if isinstance(obj, geojson.geometry.LineString):
                ret = dict(obj)

                path = coordinates  # ret['coordinates']
                mapPointList = (lambda s, x, y: (x, y))
                ret['paths'] = [[mapPointList(21781, *xy) for xy in path]]
                ret['type'] = 'esriGeometryPolyline'

                return self._cleanup(ret)

            if isinstance(obj, (geojson.MultiPoint)):
                ret = dict(obj)
                mapPointList = (lambda s, x, y: (x, y))
                points = [mapPointList(21781, *xy) for xy in coordinates]
                ret['points'] = points
                ret['type'] = 'esriGeometryMultipoint'

                return self._cleanup(ret)

            if isinstance(obj, geojson.MultiLineString):
                ret = dict(obj)

                paths = coordinates
                mapPointList = (lambda s, x, y: (x, y))
                ret['paths'] = [[mapPointList(21781, *xy) for xy in p] for p in paths]
                ret['type'] = 'esriGeometryPolyline'

                return self._cleanup(ret)

            if isinstance(obj, (geojson.Polygon, geojson.geometry.Polygon)) or geom_type == 'Polygon':
                ret = dict(obj)

                rings = coordinates
                mapPointList = (lambda s, x, y: (x, y))
                ret['rings'] = [[mapPointList(21781, *xy) for xy in ring] for ring in rings]
                ret['type'] = 'esriGeometryPolygon'

                return self._cleanup(ret)

            if isinstance(obj, geojson.MultiPolygon):
                ret = dict(obj)

                mapPointList = (lambda s, x, y: (x, y))
                rings = reduce(add, coordinates)
                ret['rings'] = [[mapPointList(21781, *xy) for xy in ring] for ring in rings]
                ret['type'] = 'esriGeometryPolygon'

                return self._cleanup(ret)

        return GeoJSONEncoder.default(self, obj)
Пример #3
0
    def default(self, obj):
         geom_type  = None
         if hasattr(obj, '__geo_interface__'):
             geom_type = dict(obj.__geo_interface__)['type']

         if isinstance(obj, (decimal.Decimal, datetime.date, datetime.datetime)):
            return str(obj)
        
         if isinstance(obj, GeoInterface):
             self.srs = obj.geometry_column().type.srid
             obj = obj.__geo_interface__
         
         if isinstance(obj, (geojson.GeoJSON)):
            ret = dict(obj)
            if 'coordinates' in ret.keys():
                coordinates = ret['coordinates']
         
            if isinstance(obj, (geojson.Feature,geojson.feature.Feature)) or geom_type == 'Feature':
                ret = dict(obj)
                geometry = self.default(obj.geometry)
                ret = dict(obj)
                ret['geometry'] = geometry
                esriType = 'esriGeometryPoint'
                if 'rings' in geometry.keys():
                    esriType ='esriGeometryPolygon'
                if 'paths' in geometry.keys():
                    esriType ='esriGeometryPolyline'
                ret['geometryType'] = esriType

                return self._cleanup(ret)
                
            if isinstance(obj, (geojson.FeatureCollection)):
                features = []
                for feature in obj.features:
                    features.append(self.default(feature))
    
                ret = dict(obj)
                ret['features'] = features
                return self._cleanup(ret)
            
            if isinstance(obj, (geojson.Point)):
               ret = dict(obj)
 
               ret['x'], ret['y'] = coordinates
               ret['type'] = 'esriGeometryPoint'
               
               return self._cleanup(ret)
            
            if isinstance(obj, geojson.geometry.LineString):
               ret = dict(obj)
               
               path = coordinates #ret['coordinates']
               mapPointList = (lambda s, x, y: (x, y))
               ret['paths'] = [[mapPointList(21781, *xy) for xy in path]]
               ret['type'] = 'esriGeometryPolyline'

               return self._cleanup(ret)             
         
            if isinstance(obj, (geojson.MultiPoint)):
               ret = dict(obj)
               mapPointList = (lambda s, x, y: (x, y))
               points = [mapPointList(21781, *xy) for xy in coordinates]
               ret['points'] =  points
               ret['type'] = 'esriGeometryMultipoint'
               
               return self._cleanup(ret)
            
              
            if isinstance(obj, geojson.MultiLineString):
               ret = dict(obj)
    
               paths = coordinates 
               mapPointList = (lambda s, x, y: (x, y))
               ret['paths'] = [[mapPointList(21781, *xy) for xy in path] for path in paths]
               ret['type'] = 'esriGeometryPolyline'
               
               return self._cleanup(ret)
               
            if isinstance(obj, (geojson.Polygon, geojson.geometry.Polygon))  or geom_type  == 'Polygon':
               ret = dict(obj)
               
               rings = coordinates 
               mapPointList = (lambda s, x, y: (x, y))
               ret['rings'] =  [[mapPointList(21781, *xy) for xy in ring] for ring in rings]
               ret['type'] = 'esriGeometryPolygon'
               
               return self._cleanup(ret)
               
            if isinstance(obj, geojson.MultiPolygon):
               ret = dict(obj)
               
               mapPointList = (lambda s, x, y: (x, y))
               rings = reduce(add, coordinates)
               ret['rings'] = [[mapPointList(21781, *xy) for xy in ring] for ring in rings]
               ret['type'] = 'esriGeometryPolygon'

                         
               return self._cleanup(ret)
         
         return GeoJSONEncoder.default(self, obj)