def project(x, y, fromproj=LongLat(), toproj=LongLat()): """ Project geographic coordinates from one projection to another. :param x: (*array_like*) X coordinate values for projection. :param y: (*array_like*) Y coordinate values for projection. :param fromproj: (*ProjectionInfo*) From projection. Default is longlat projection. :param toproj: (*ProjectionInfo*) To projection. Default is longlat projection. :returns: (*array_like*, *array_like*) Projected geographic coordinates. """ if isinstance(fromproj, str): fromproj = ProjectionInfo.factory(fromproj) if isinstance(toproj, str): toproj = ProjectionInfo.factory(toproj) if isinstance(x, (tuple, list)): x = array(x) if isinstance(y, (tuple, list)): y = array(y) if isinstance(x, np.NDArray): outxy = ArrayUtil.reproject(x.asarray(), y.asarray(), fromproj, toproj) return np.NDArray(outxy[0]), np.NDArray(outxy[1]) else: inpt = PointD(x, y) outpt = Reproject.reprojectPoint(inpt, fromproj, toproj) return outpt.X, outpt.Y
def maskin(data, mask, x=None, y=None): """ Maskin data by polygons - NaN values of elements inside polygons. :param data: (*array_like*) Array data for maskout. :param mask: (*list*) Polygon list as maskin borders. :param x: (*array_like*) X coordinate array. :param y: (*array_like*) Y coordinate array. :returns: (*array_like*) Maskined data array. """ if mask is None: return data elif isinstance(mask, np.NDArray): r = GeoComputation.maskin(data._array, mask._array) if isinstance(data, DimArray): return DimArray(r, data.dims, data.fill_value, data.proj) else: return np.NDArray(r) if x is None or y is None: if isinstance(data, DimArray): x = data.dimvalue(data.ndim - 1) y = data.dimvalue(data.ndim - 2) else: return None if not isinstance(mask, (list, ArrayList)): mask = [mask] r = GeoComputation.maskin(data._array, x._array, y._array, mask) if isinstance(data, DimArray): return DimArray(r, data.dims, data.fill_value, data.proj) else: return np.NDArray(r)
def arrayinpolygon(a, polygon, x=None, y=None): ''' Set array element value as 1 if inside a polygon or set value as -1. :param a: (*array_like*) The array. :param polygon: (*PolygonShape*) The polygon. :param x: (*float*) X coordinate of the point. Default is ``None``, for DimArray :param y: (*float*) Y coordinate of the point. Default is ``None``, for DimArray :returns: (*array_like*) Result array. ''' if isinstance(a, DimArray): if x is None or y is None: x = self.dimvalue(1) y = self.dimvalue(0) if not x is None and not y is None: if isinstance(polygon, tuple): x_p = polygon[0] y_p = polygon[1] if isinstance(x_p, np.NDArray): x_p = x_p.aslist() if isinstance(y_p, np.NDArray): y_p = y_p.aslist() return np.NDArray(GeoComputation.inPolygon(a.asarray(), x.aslist(), y.aslist(), x_p, y_p)) else: if isinstance(polygon, MILayer): polygon = polygon.layer return np.NDArray(GeoComputation.inPolygon(a.asarray(), x.aslist(), y.aslist(), polygon)) else: return None
def inpolygon(x, y, polygon): ''' Check if x/y points are inside a polygon or not. :param x: (*array_like*) X coordinate of the points. :param y: (*array_like*) Y coordinate of the points. :param polygon: (*PolygonShape list*) The polygon list. :returns: (*boolean array*) Inside or not. ''' if isinstance(x, numbers.Number): return GeoComputation.pointInPolygon(polygon, x, y) if isinstance(x, (list, tuple)): x = np.array(x) if isinstance(y, (list, tuple)): y = np.array(y) if isinstance(polygon, tuple): x_p = polygon[0] y_p = polygon[1] if isinstance(x_p, (list, tuple)): x_p = np.array(x_p) if isinstance(y_p, (list, tuple)): y_p = np.array(y_p) return np.NDArray(GeoComputation.inPolygon(x._array, y._array, x_p._array, y_p._array)) else: if isinstance(polygon, MILayer): polygon = polygon.shapes() elif isinstance(polygon, PolygonShape): polygon = [polygon] return np.NDArray(GeoComputation.inPolygon(x._array, y._array, polygon))
def rmaskin(data, x, y, mask): """ Maskin data by polygons - the elements inside polygons will be removed :param data: (*array_like*) Array data for maskin. :param x: (*array_like*) X coordinate array. :param y: (*array_like*) Y coordinate array. :param mask: (*list*) Polygon list as mask borders. :returns: (*list*) Masked data, x and y array list. """ if not isinstance(mask, (list, ArrayList)): mask = [mask] r = GeoComputation.maskin_Remove(data._array, x._array, y._array, mask) return np.NDArray(r[0]), np.NDArray(r[1]), np.NDArray(r[2])
def rmaskout(data, x, y, mask): """ Maskout data by polygons - the elements outside polygons will be removed :param data: (*array_like*) Array data for maskout. :param x: (*array_like*) X coordinate array. :param y: (*array_like*) Y coordinate array. :param mask: (*list*) Polygon list as maskout borders. :returns: (*list*) Maskouted data, x and y array list. """ if not isinstance(mask, (list, ArrayList)): mask = [mask] r = ArrayMath.maskout_Remove(data.asarray(), x.asarray(), y.asarray(), mask) return np.NDArray(r[0]), np.NDArray(r[1]), np.NDArray(r[2])
def reproject(a, x=None, y=None, toproj=None, method='bilinear'): """ Project array :param a: (*array*) Input array :param x: To x coordinates. :param y: To y coordinates. :param toproj: To projection. :param method: Interpolation method: ``bilinear`` or ``neareast`` . :returns: (*NDArray*) Projected array """ yy = a.dims[a.ndim - 2].getDimValue() xx = a.dims[a.ndim - 1].getDimValue() fromproj = ProjectionInfo.factory(a.proj) if toproj is None: toproj = fromproj if x is None or y is None: pr = Reproject.reproject(a._array, xx, yy, fromproj, toproj) r = pr[0] x = pr[1] y = pr[2] dims = a.dims ydim = Dimension(DimensionType.Y) ydim.setDimValues(np.NDArray(y).aslist()) dims[-2] = ydim xdim = Dimension(DimensionType.X) xdim.setDimValues(np.NDArray(x).aslist()) dims[-1] = xdim rr = DimArray(np.NDArray(r), dims, a.fill_value, toproj) return rr if method == 'bilinear': method = ResampleMethods.Bilinear else: method = ResampleMethods.NearestNeighbor if isinstance(x, list): r = Reproject.reproject(a._array, xx, yy, x, y, fromproj, toproj, a.fill_value, method) elif isinstance(x, NDArray): if x.ndim == 1: r = Reproject.reproject(a._array, xx, yy, x.aslist(), y.aslist(), fromproj, toproj, a.fill_value, method) else: r = Reproject.reproject(a._array, xx, yy, x.asarray(), y.asarray(), fromproj, toproj, a.fill_value, method) else: r = Reproject.reproject(a._array, xx, yy, x.asarray(), y.asarray(), fromproj, toproj, a.fill_value, method) #r = Reproject.reproject(self.array, xx, yy, x.asarray(), y.asarray(), self.proj, toproj, self.fill_value, method) return NDArray(r)
def geotiffread(filename): ''' Return data array from a GeoTiff data file. :param filename: (*string*) The GeoTiff file name. :returns: (*MIArray*) Readed data array. ''' geotiff = GeoTiff(filename) geotiff.read() r = geotiff.readArray() return np.NDArray(r)