Beispiel #1
0
  def __init__(self, name=None, fields=[], ft=None, 
               uri='http://geoscript.org/feature'):
    self._type = ft

    if name and fields:
      # name and fields specified directly, generate gt feature type 
      # from list
      tb = SimpleFeatureTypeBuilder()
      tb.setName(NameImpl(name))
      tb.setNamespaceURI(uri)
      for fld in fields:
        if isinstance(fld, Field):
          name, typ, prj = fld.name, fld.typ, fld.proj
        else:
          name, typ = fld[0], fld[1]
          prj = None
          if issubclass(typ, geom.Geometry):
            # look for srs/crs info
            if len(fld) > 2:
              prj = proj.Projection(fld[2])
        prj = prj if prj else proj.Projection('EPSG:4326')
        if prj:
          tb.crs(prj._crs)
          
        # we call map() here to avoid setting the type binding to a Python
        # (eg: PyInteger) type, but rather a native java type (Integer)
        tb.add(name, core.map(typ))

      self._type = tb.buildFeatureType()
        
    elif ft:
      # gt feature type specified directly
      self._type = ft
    else:
      raise Exception('No fields specified for feature type.')
Beispiel #2
0
    def get_shapefile_writer(clz, shapefile="", driver='ESRI Shapefile', crs=None, 
                             schema=None):

        type_builder = SimpleFeatureTypeBuilder()
        type_builder.setName("custom_feature")
        crs = gis_util.get_crs(crs)
        type_builder.setCRS(crs)
        geom_type = geom_type_mappings[schema['geometry']]
        type_builder.add('geometry', geom_type, crs)
        for prop, property_type in schema.get('properties', {}).items():
            type_builder.add(prop, property_type_mappings[property_type])
        feature_type = type_builder.buildFeatureType()

        feature_builder = SimpleFeatureBuilder(feature_type)

        class ShapefileWriter(object):
            def __init__(self):
                # Set up feature store.
                url = File(shapefile).toURI().toURL()
                self.ds = ShapefileDataStore(url)
                self.ds.createSchema(feature_type)
                type_name = self.ds.getTypeNames()[0]
                self.fs = self.ds.getFeatureSource(type_name)
                self.transaction = DefaultTransaction("create")

                # Setup feature list.
                self.features = []

            def write(self, record):
                # Process geometry.
                geom = gis_util.geojson_to_shape(record['geometry'])
                feature_builder.set('geometry', geom._jgeom)

                # Process properties.
                for prop, value in record.get('properties', {}).items():
                    feature_builder.set(prop, value)

                # Create feature.
                id_ = record.get('id')
                if id_ is not None:
                    id_ = str(id_)
                feature = feature_builder.buildFeature(id_)

                # Add to feature list.
                self.features.append(feature)

            def close(self):
                # Add features to feature store.
                fc = ListFeatureCollection(feature_type, self.features)
                self.fs.setTransaction(self.transaction)
                self.fs.addFeatures(fc)
                self.transaction.commit()
                self.transaction.close()

        return ShapefileWriter()
Beispiel #3
0
    def __init__(self, name=None, fields=[], ft=None):
        self._type = ft

        if name and fields:
            # name and fields specified directly, generate gt feature type
            # from list
            tb = SimpleFeatureTypeBuilder()
            tb.setName(NameImpl(name))
            for fld in fields:
                if isinstance(fld, Field):
                    name, typ, prj = fld.name, fld.typ, fld.proj
                else:
                    name, typ = fld[0], fld[1]
                    prj = None
                    if issubclass(typ, geom.Geometry):
                        # look for srs/crs info
                        if len(fld) > 2:
                            prj = proj.Projection(fld[2])
                if prj:
                    tb.crs(prj._crs)

                # we call map() here to avoid setting the type binding to a Python
                # (eg: PyInteger) type, but rather a native java type (Integer)
                tb.add(name, core.map(typ))

            self._type = tb.buildFeatureType()

        elif ft:
            # gt feature type specified directly
            self._type = ft
        else:
            raise Exception('No fields specified for feature type.')
Beispiel #4
0
    def get_shapefile_writer(clz,
                             shapefile="",
                             driver='ESRI Shapefile',
                             crs=None,
                             schema=None):

        type_builder = SimpleFeatureTypeBuilder()
        type_builder.setName("custom_feature")
        crs = gis_util.get_crs(crs)
        type_builder.setCRS(crs)
        geom_type = geom_type_mappings[schema['geometry']]
        type_builder.add('geometry', geom_type, crs)
        for prop, property_type in schema.get('properties', {}).items():
            type_builder.add(prop, property_type_mappings[property_type])
        feature_type = type_builder.buildFeatureType()

        feature_builder = SimpleFeatureBuilder(feature_type)

        class ShapefileWriter(object):
            def __init__(self):
                # Set up feature store.
                url = File(shapefile).toURI().toURL()
                self.ds = ShapefileDataStore(url)
                self.ds.createSchema(feature_type)
                type_name = self.ds.getTypeNames()[0]
                self.fs = self.ds.getFeatureSource(type_name)
                self.transaction = DefaultTransaction("create")

                # Setup feature list.
                self.features = []

            def write(self, record):
                # Process geometry.
                geom = gis_util.geojson_to_shape(record['geometry'])
                feature_builder.set('geometry', geom._jgeom)

                # Process properties.
                for prop, value in record.get('properties', {}).items():
                    feature_builder.set(prop, value)

                # Create feature.
                id_ = record.get('id')
                if id_ is not None:
                    id_ = str(id_)
                feature = feature_builder.buildFeature(id_)

                # Add to feature list.
                self.features.append(feature)

            def close(self):
                # Add features to feature store.
                fc = ListFeatureCollection(feature_type, self.features)
                self.fs.setTransaction(self.transaction)
                self.fs.addFeatures(fc)
                self.transaction.commit()
                self.transaction.close()

        return ShapefileWriter()