def reproject(self, prj, name=None, chunk=1000): """ Reprojects a layer. *prj* is the destination :class:`Projection <geoscript.proj.Projection>` *name* is the optional name as a ``str`` to assign to the resulting reprojected layer. This method returns a newly reprojected layer. The new layer is create within the containing workspace of the original layer. >>> from geoscript import geom >>> l = Layer() >>> l.proj = 'epsg:4326' >>> l.add([geom.Point(-111, 45.7)]) >>> >>> l2 = l.reproject('epsg:26912') >>> l2.proj.id 'EPSG:26912' >>> [f.geom.round() for f in l2.features()] [POINT (500000 5060716)] """ prj = proj.Projection(prj) name = name or Layer._newname() # reproject the schema rschema = self.schema.reproject(prj, name) # create the reprojected layer rlayer = self.workspace.create(schema=rschema) # create a query specifying that feautres should be reprojected q = DefaultQuery(self.name, Filter.PASS._filter) if self.proj: q.coordinateSystem = self.proj._crs q.coordinateSystemReproject = prj._crs # loop through features and add to new reprojeced layer fit = self._source.getFeatures(q).features() try: while True: features = readFeatures(fit, self._source.getSchema(), chunk) if features.isEmpty(): break rlayer._source.addFeatures(features) finally: fit.close() return rlayer
def add(self, layer, name=None, chunk=1000): """ Adds an existing layer to the workspace. *layer* is the :class:`Layer <geoscript.layer.layer.Layer>` to add. *name* is the optional name as a ``str`` to assign to the layer when copied into the workspace. >>> ws = Workspace() >>> ws.layers() [] >>> from geoscript.layer import Layer >>> l = Layer('foo') >>> l = ws.add(l) >>> ws.layers() ['foo'] """ name = name if name else layer.name try: self.get(name) raise Exception('Layer named "%s" already exists.' % name) except KeyError: if layer.proj: flds = [] for fld in layer.schema.fields: flds.append((fld.name, fld.typ, layer.proj ) if issubclass(fld.typ, geom.Geometry) else ( fld.name, fld.typ)) else: flds = [(fld.name, fld.typ) for fld in layer.schema.fields] l = self.create(name, flds) # add all the features it = layer._source.getFeatures().features() try: while True: features = readFeatures(it, layer._source.getSchema(), chunk) if features.isEmpty(): break l._source.addFeatures(features) return l finally: it.close()
def add(self, layer, name=None, chunk=1000): """ Adds an existing layer to the workspace. *layer* is the :class:`Layer <geoscript.layer.layer.Layer>` to add. *name* is the optional name as a ``str`` to assign to the layer when copied into the workspace. >>> ws = Workspace() >>> ws.layers() [] >>> from geoscript.layer import Layer >>> l = Layer('foo') >>> l = ws.add(l) >>> ws.layers() ['foo'] """ name = name if name else layer.name try: self.get(name) raise Exception('Layer named "%s" already exists.' % name) except KeyError: if layer.proj: flds = [] for fld in layer.schema.fields: flds.append( (fld.name, fld.typ, layer.proj) if issubclass(fld.typ, geom.Geometry) else (fld.name, fld.typ) ) else: flds = [(fld.name, fld.typ) for fld in layer.schema.fields] l = self.create(name, flds) # add all the features it = layer._source.getFeatures().features() try: while True: features = readFeatures(it, layer._source.getSchema(), chunk) if features.isEmpty(): break l._source.addFeatures(features) return l finally: it.close()