Ejemplo n.º 1
0
    def transform(self, config='', **kwargs):
        """ Transform the data using the config file """

        if 'crs_target' in kwargs:
            crs_target = kwargs.pop('crs_target', 'EPSG:4326')
            self.epsg = crs_target
        else:
            crs_target = 'EPSG:4326'
            self.epsg = 'EPSG:4326'
            self.wkt = osr.SRS_WKT_WGS84

        crs_target = get_coordinate_rerefence_system(extract_epsg(crs_target))
        crs_source = get_coordinate_rerefence_system(extract_epsg(self.crs))

        if config:
            config = Models(config).config
            self.properties = config_to_properties(config)

        for x, feature in enumerate(self.features):
            # Detect if correct geometry (Multipoints > Point)
            feature = confirm_geometry(feature)

            # Reproject data to target projection (crs_target=4326)
            if kwargs.get('reproject', False):
                reproject(feature, crs_source, crs_target)

            # Transform Columns
            if config:
                feature = transform_columns(feature, config, **kwargs)

            # Save feature to self
            self[x] = feature
Ejemplo n.º 2
0
    def read_geojson(self, infile, **kwargs):
        """Reads a GeoJSON and gives the results in GeoJSON format
        Speed = 0.091ms/feature"""

        logging.info('Reading GeoJSON: %s' % infile)
        if isinstance(infile, dict):
            geojson = infile
        else:
            with open(infile) as f:
                geojson = json.load(f)

        if 'type' not in geojson:
            raise ValueError('GeoJSON must contain a [type] "FeatureCollection" or "Feature"')
        if 'crs' not in geojson:
            logging.warning('Coordinate Reference System was not detected (default=EPSG:4326)')
            self.epsg = 'EPSG:4326'
        else:
            self.epsg = 'EPSG:{}'.format(extract_epsg(geojson['crs']))

        # Read GeoJSON Feature Collection
        if geojson['type'] == 'FeatureCollection':
            if not geojson['features']:
                raise ValueError('FeatureCollection has [0] features.')
            features = geojson['features']

        # Read Single GeoJSON Feature
        elif geojson['type'] == 'Feature':
            features = [geojson]

        properties = set()
        self.geometry = set()

        for feature in features:
            # Add unique attribute keys to properties
            if 'properties' in feature:
                properties.update(feature['properties'].keys())

            # Only add features with geometry
            if feature.get('geometry'):
                feature = confirm_geometry(feature)
                self.features.append(feature)
                self.geometry.update([feature['geometry']['type']])
            else:
                logging.warning('Could not find [geometry] in feature.')

        # Creating basic properties for attribute table when building a shapefile.
        self.properties = dict((key, 'str') for key in properties)