def _check_src(self, src): """ Basic check of source elements (sequence of points or polygons). - array cast of source elements - create ogr_src datasource/layer holding src points/polygons - transforming source grid points/polygons to ogr.geometries on ogr.layer """ ogr_src = ogr_create_datasource('Memory', 'out') try: # is it ESRI Shapefile? ds_in, tmp_lyr = io.open_shape(src, driver=ogr.GetDriverByName('ESRI Shapefile')) ogr_src_lyr = ogr_src.CopyLayer(tmp_lyr, self._name) if self._srs is None: self._srs = ogr_src_lyr.GetSpatialRef() except IOError: # no ESRI shape file raise # all failed? then it should be sequence or numpy array except RuntimeError: src = np.array(src) # create memory datasource, layer and create features if src.ndim == 2: geom_type = ogr.wkbPoint # no Polygons, just Points else: geom_type = ogr.wkbPolygon fields = [('index', ogr.OFTInteger)] ogr_create_layer(ogr_src, self._name, srs=self._srs, geom_type=geom_type, fields=fields) ogr_add_feature(ogr_src, src, name=self._name) return ogr_src
def load_vector(self, filename): """ Load source/target grid points/polygons into in-memory Shapefile Parameters ---------- filename : string path to vector file """ # get input file handles ds_in, tmp = io.open_shape(filename) # claim memory driver drv_in = ogr.GetDriverByName('Memory') # create all DataSources self.src = DataSource(name='src') self.src.ds = drv_in.CreateDataSource('src') self.trg = DataSource(name='trg') self.trg.ds = drv_in.CreateDataSource('trg') self.dst = DataSource(name='dst') self.dst.ds = drv_in.CreateDataSource('dst') # copy all layers ogr_copy_layer_by_name(ds_in, "src", self.src.ds) ogr_copy_layer_by_name(ds_in, "trg", self.trg.ds) ogr_copy_layer_by_name(ds_in, "dst", self.dst.ds) # get spatial reference object self._srs = self.src.ds.GetLayer().GetSpatialRef() self.src._srs = self.src.ds.GetLayer().GetSpatialRef() self.trg._srs = self.trg.ds.GetLayer().GetSpatialRef() self.dst._srs = self.trg.ds.GetLayer().GetSpatialRef() # flush everything del ds_in
def load_vector(self, filename): """ Load source/target grid points/polygons into in-memory Shapefile Parameters ---------- filename : string path to vector file """ # get input file handles ds_in, tmp = io.open_shape(filename) # create all DataSources self.src = DataSource(name='src') self.src.ds = io.gdal_create_dataset('Memory', 'src', gdal_type=gdal.OF_VECTOR) self.trg = DataSource(name='trg') self.trg.ds = io.gdal_create_dataset('Memory', 'trg', gdal_type=gdal.OF_VECTOR) self.dst = DataSource(name='dst') self.dst.ds = io.gdal_create_dataset('Memory', 'dst', gdal_type=gdal.OF_VECTOR) # copy all layers georef.ogr_copy_layer_by_name(ds_in, "src", self.src.ds) georef.ogr_copy_layer_by_name(ds_in, "trg", self.trg.ds) georef.ogr_copy_layer_by_name(ds_in, "dst", self.dst.ds) # get spatial reference object self._srs = self.src.ds.GetLayer().GetSpatialRef() self.src._srs = self.src.ds.GetLayer().GetSpatialRef() self.trg._srs = self.trg.ds.GetLayer().GetSpatialRef() self.dst._srs = self.trg.ds.GetLayer().GetSpatialRef() # flush everything del ds_in