def test_geometry(self): import shapely.geometry as shpg g = Grid(nxny=(3, 3), dxdy=(1, 1), x0y0=(0, 0), proj=wgs84, pixel_ref='corner') p = shpg.Polygon([(1.5, 1.), (2., 1.5), (1.5, 2.), (1., 1.5)]) o = gis.transform_geometry(p, to_crs=g) assert_allclose(p.exterior.coords, o.exterior.coords) q = gis.transform_geometry(o, crs=g) assert_allclose(p.exterior.coords, q.exterior.coords) o = gis.transform_geometry(p, to_crs=g.center_grid) totest = np.array(o.exterior.coords) + 0.5 assert_allclose(p.exterior.coords, totest) x, y = g.corner_grid.xy_coordinates p = shpg.MultiPoint( [shpg.Point(i, j) for i, j in zip(x.flatten(), y.flatten())]) o = gis.transform_geometry(p, to_crs=g.proj) assert_allclose([_p.coords for _p in o], [_p.coords for _p in p])
def set_roi(self, shape=None, geometry=None, crs=wgs84, grid=None, corners=None, noerase=False): """Set a region of interest for the dataset. If set succesfully, a ROI is simply a mask of the same size as the dataset's grid, obtained with the .roi attribute. I haven't decided yet if the data should be masekd out when a ROI has been set. Parameters ---------- shape: path to a shapefile geometry: a shapely geometry crs: the crs of the geometry grid: a Grid object corners: a ((x0, y0), (x1, y1)) tuple of the corners of the square to subset the dataset to. The coordinates are not expressed in wgs84, set the crs keyword noerase: set to true in order to add the new ROI to the previous one """ # The rois are always defined on the original grids, but of course # we take that into account when a subset is set (see roi # decorator below) ogrid = self._ogrid # Initial mask if noerase and (self.roi is not None): mask = self.roi else: mask = np.zeros((ogrid.ny, ogrid.nx), dtype=np.int16) # Several cases if shape is not None: gdf = sio.read_shapefile(shape) gis.transform_geopandas(gdf, to_crs=ogrid.corner_grid, inplace=True) if rasterio is None: raise ImportError('This feature needs rasterio') from rasterio.features import rasterize with rasterio.Env(): mask = rasterize(gdf.geometry, out=mask) if geometry is not None: geom = gis.transform_geometry(geometry, crs=crs, to_crs=ogrid.corner_grid) if rasterio is None: raise ImportError('This feature needs rasterio') from rasterio.features import rasterize with rasterio.Env(): mask = rasterize(np.atleast_1d(geom), out=mask) if grid is not None: _tmp = np.ones((grid.ny, grid.nx), dtype=np.int16) mask = ogrid.map_gridded_data(_tmp, grid, out=mask).filled(0) if corners is not None: cgrid = self._ogrid.center_grid xy0, xy1 = corners x0, y0 = cgrid.transform(*xy0, crs=crs, nearest=True) x1, y1 = cgrid.transform(*xy1, crs=crs, nearest=True) mask[np.min([y0, y1]):np.max([y0, y1])+1, np.min([x0, x1]):np.max([x0, x1])+1] = 1 self.roi = mask
def set_geometry(self, geometry=None, crs=wgs84, text=None, text_delta=(0.01, 0.01), text_kwargs=dict(), **kwargs): """Adds any Shapely geometry to the map. If called without arguments, it removes all previous geometries. Parameters ---------- geometry: a Shapely gometry object (must be a scalar!) crs: the associated coordinate reference system (default wgs84) text: if you want to add a text to the geometry (it's position is based on the geometry's centroid) text_delta: it can be useful to shift the text of a certain amount when annotating points. units are percentage of data coordinates. text_kwargs: the keyword arguments to pass to the test() function kwargs: any keyword associated with the geometry's plotting function: - Point: all keywords accepted by scatter(): marker, s, edgecolor, facecolor... - Line: all keywords accepted by plot(): color, linewidth... - Polygon: all keywords accepted by PathPatch(): color, edgecolor, facecolor, linestyle, linewidth, alpha... """ # Reset? if geometry is None: self._geometries = [] return # Transform geom = gis.transform_geometry(geometry, crs=crs, to_crs=self.grid.center_grid) # Text if text is not None: x, y = geom.centroid.xy x = x[0] + text_delta[0] * self.grid.nx sign = self.grid.dy / np.abs(self.grid.dy) y = y[0] + text_delta[1] * self.grid.ny * sign self.set_text(x, y, text, crs=self.grid.center_grid, **text_kwargs) # Save if 'Multi' in geom.type: for g in geom: self._geometries.append((g, kwargs)) # dirty solution: I should use collections instead if 'label' in kwargs: kwargs = kwargs.copy() del kwargs['label'] else: self._geometries.append((geom, kwargs))
def test_geometry(self): import shapely.geometry as shpg g = Grid(nxny=(3, 3), dxdy=(1, 1), ll_corner=(0, 0), proj=wgs84, pixel_ref='corner') p = shpg.Polygon([(1.5, 1.), (2., 1.5), (1.5, 2.), (1., 1.5)]) o = gis.transform_geometry(p, to_crs=g) assert_allclose(p.exterior.coords, o.exterior.coords) o = gis.transform_geometry(p, to_crs=g.center_grid) totest = np.array(o.exterior.coords) + 0.5 assert_allclose(p.exterior.coords, totest) x, y = g.corner_grid.xy_coordinates p = shpg.MultiPoint([shpg.Point(i, j) for i, j in zip(x.flatten(), y.flatten())]) o = gis.transform_geometry(p, to_crs=g.proj) assert_allclose([_p.coords for _p in o], [_p.coords for _p in p])
def set_roi(self, shape=None, geometry=None, crs=wgs84, grid=None, corners=None, noerase=False): """Set a region of interest for the dataset. If set succesfully, a ROI is simply a mask of the same size as the dataset's grid, obtained with the .roi attribute. I haven't decided yet if the data should be masekd out when a ROI has been set. Parameters ---------- shape: path to a shapefile geometry: a shapely geometry crs: the crs of the geometry grid: a Grid object corners: a ((x0, y0), (x1, y1)) tuple of the corners of the square to subset the dataset to. The coordinates are not expressed in wgs84, set the crs keyword noerase: set to true in order to add the new ROI to the previous one """ # The rois are always defined on the original grids, but of course # we take that into account when a subset is set (see roi # decorator below) ogrid = self._ogrid # Initial mask if noerase and (self.roi is not None): mask = self.roi else: mask = np.zeros((ogrid.ny, ogrid.nx), dtype=np.int16) # Several cases if shape is not None: gdf = sio.read_shapefile(shape) gis.transform_geopandas(gdf, to_crs=ogrid.corner_grid, inplace=True) with rasterio.Env(): mask = features.rasterize(gdf.geometry, out=mask) if geometry is not None: geom = gis.transform_geometry(geometry, crs=crs, to_crs=ogrid.corner_grid) with rasterio.Env(): mask = features.rasterize(np.atleast_1d(geom), out=mask) if grid is not None: _tmp = np.ones((grid.ny, grid.nx), dtype=np.int16) mask = ogrid.map_gridded_data(_tmp, grid, out=mask).filled(0) if corners is not None: cgrid = self._ogrid.center_grid xy0, xy1 = corners x0, y0 = cgrid.transform(*xy0, crs=crs, nearest=True) x1, y1 = cgrid.transform(*xy1, crs=crs, nearest=True) mask[np.min([y0, y1]):np.max([y0, y1])+1, np.min([x0, x1]):np.max([x0, x1])+1] = 1 self.roi = mask
def set_geometry(self, geometry=None, crs=wgs84, text=None, text_delta=(0.01, 0.01), text_kwargs=dict(), **kwargs): """Adds any Shapely geometry to the map. If called without arguments, it removes all previous geometries. Parameters ---------- geometry: a Shapely gometry object (must be a scalar!) crs: the associated coordinate reference system (default wgs84) text: if you want to add a text to the geometry (it's position is based on the geometry's centroid) text_delta: it can be useful to shift the text of a certain amount when annotating points. units are percentage of data coordinates. text_kwargs: the keyword arguments to pass to the test() function kwargs: any keyword associated with the geometrie's plotting function:: - Point: all keywords accepted by scatter(): marker, s, edgecolor, facecolor... - Line: all keywords accepted by plot(): color, linewidth... - Polygon: all keywords accepted by PathPatch(): color, edgecolor, facecolor, linestyle, linewidth, alpha... """ # Reset? if geometry is None: self._geometries = [] return # Transform geom = gis.transform_geometry(geometry, crs=crs, to_crs=self.grid.center_grid) # Text if text is not None: x, y = geom.centroid.xy x = x[0] + text_delta[0] * self.grid.nx sign = self.grid.dy / np.abs(self.grid.dy) y = y[0] + text_delta[1] * self.grid.ny * sign self.set_text(x, y, text, crs=self.grid.center_grid, **text_kwargs) # Save if 'Multi' in geom.type: for g in geom: self._geometries.append((g, kwargs)) # dirty solution: I should use collections instead if 'label' in kwargs: kwargs = kwargs.copy() del kwargs['label'] else: self._geometries.append((geom, kwargs))