Beispiel #1
0
    def from_geojson(self, geojson_data, id_field=None, update=False):
        """
        Import geojson raw data in a layer
        Args:
            geojson_data(str): must be raw text json data
        """

        geojson = json.loads(geojson_data)
        projection = geojson.get('crs', {}).get('properties',
                                                {}).get('name', None)
        if projection and not self.is_projection_allowed(projection):
            raise GEOSException(f'GeoJSON projection {projection} must be in '
                                f'{ACCEPTED_PROJECTIONS}')

        if update:
            self.features.all().delete()
        for feature in geojson.get('features', []):
            properties = feature.get('properties', {})
            identifier = properties.get(id_field, uuid.uuid4())
            self.features.update_or_create(
                layer=self,
                identifier=identifier,
                defaults={
                    'properties': properties,
                    'geom': GEOSGeometry(json.dumps(feature.get('geometry'))),
                })
        def __init__(self, type_input):
            "Figure out the correct OGR Type based upon the input."
            if isinstance(type_input, OGRGeomType):
                num = type_input.num
            elif isinstance(type_input, str):
                type_input = type_input.lower()
                if type_input == 'geometry':
                    type_input = 'unknown'
                num = self._str_types.get(type_input)
                if num is None:
                    raise GEOSException('Invalid OGR String Type "%s"' % type_input)
            elif isinstance(type_input, int):
                if type_input not in self._types:
                    raise GEOSException('Invalid OGR Integer Type: %d' % type_input)
                num = type_input
            else:
                raise TypeError('Invalid OGR input type given.')

                # Setting the OGR geometry type number.
            self.num = num
Beispiel #3
0
    def _fiona_shape_projection(self, shapefile):
        """ Return projection in EPSG format or raw Proj format extracted from
            shape
        """
        proj = shapefile.crs

        if not proj:
            raise GEOSException('Your shapefile does not have projection')

        if len(proj) == 1 or (len(proj) == 2 and proj.get('no_defs') is True):
            return proj.get('init')
        else:
            return fiona.crs.to_string(proj)