def t_open_vector_error_handling(mocker): m_settings = mocker.patch.object(utils, 'settings') m_settings.return_value.DATABASES = {'fakedbsetting': { 'NAME': 'fake-db', 'USER': '******', 'PASSWORD': '******', 'HOST': 'fake-oka', 'PORT': '5432', }} m_GeoVector = mocker.patch.object(utils, 'GeoVector') fname = 'fakedbsetting:bar' utils.open_vector(fname) m_GeoVector.return_value.SetPrimaryKey.assert_called_once_with("")
def vector(cls): """ Get GeoVector of sensor grid """ # TODO = update to use gippy.GeoVector # check location from settings vpath = os.path.join('/etc/gips', cls.name) vector = open_vector(cls.repo().get('vector', 'tiles.shp'), path=vpath) return GeoVector(vector.Filename(), vector.LayerName())
def factory(cls, dataclass, site=None, key='', where='', tiles=None, pcov=0.0, ptile=0.0): """ Create array of SpatialExtent instances """ if site is None and tiles is None: # raise Exception('Site geometry and/or tile ids required') # TODO: make this better. It only returns the tiles you have without telling # you that is what it is doing. tiles = dataclass.Asset.Repository.find_tiles() extents = [] if site is None: # tiles are spatial extent for t in tiles: extents.append( cls(dataclass, tiles=[t], pcov=pcov, ptile=ptile)) else: features = open_vector(site, key, where) for f in features: extents.append( cls(dataclass, feature=f, tiles=tiles, pcov=pcov, ptile=ptile)) return extents
def factory(cls, dataclass, site=None, key='', where='', tiles=None, pcov=0.0, ptile=0.0): """ Create array of SpatialExtent instances """ if site is None and tiles is None: # raise Exception('Site geometry and/or tile ids required') tiles = dataclass.Asset.Repository.find_tiles() extents = [] if site is None: # tiles are spatial extent for t in tiles: extents.append( cls(dataclass, tiles=[t], pcov=pcov, ptile=ptile)) else: features = open_vector(site, key, where) for f in features: extents.append( cls(dataclass, feature=f, tiles=tiles, pcov=pcov, ptile=ptile)) return extents
def tile_bounds(cls, tile): """ Get the bounds of the tile (in same units as tiles vector) """ vector = open_vector(cls.get_setting('tiles')) features = vector.where('tileid', tile) if len(features) != 1: raise Exception('there should be a single tile with id %s' % tile) extent = features[0].Extent() return [extent.x0(), extent.y0(), extent.x1(), extent.y1()]
def factory(cls, dataclass, site=None, rastermask=None, key='', where='', tiles=None, pcov=0.0, ptile=0.0): """Create array of SpatialExtent instances. site is a path to a shapefile. rastermask is a path to a specially-constructed tif (other formats?). It is used as a mask that defines the spatial extent (how?). key is used as the PrimaryKey of `site` (gippy weirdness I dunno). where is an expression-like string used to filter the shapes in `site`. pcov and ptile are percentage thresholds used as filters (TODO specifics). """ if tiles is not None: tiles = [dataclass.normalize_tile_string(t) for t in tiles] if site is None and rastermask is None and tiles is None: # raise Exception('Site geometry and/or tile ids required') # TODO: make this better. It only returns the tiles you have without telling # you that is what it is doing. tiles = dataclass.Asset.Repository.find_tiles() extents = [] if site is None and rastermask is None: # tiles are spatial extent for t in tiles: extents.append(cls(dataclass, tiles=[t], pcov=pcov, ptile=ptile)) else: # these are set mutually exclusive in parsers, but could # both be passed from elsewhere. for now, preferentially # use rastermask if provided. if rastermask is not None: vectorfile = os.path.join(os.path.dirname(rastermask), os.path.basename(rastermask)[:-4] + '.shp') features = open_vector(utils.vectorize(rastermask, vectorfile), where='DN=1') for f in features: extents.append(cls(dataclass, feature=f, rastermask=rastermask, tiles=tiles, pcov=pcov, ptile=ptile)) else: features = open_vector(site, key, where) for f in features: extents.append(cls(dataclass, feature=f, rastermask=rastermask, tiles=tiles, pcov=pcov, ptile=ptile)) return extents
def factory(cls, dataclass, site=None, key='', where='', tiles=None, pcov=0.0, ptile=0.0): """ Create array of SpatialExtent instances """ if site is None and tiles is None: # raise Exception('Site geometry and/or tile ids required') tiles = dataclass.Asset.Repository.find_tiles() extents = [] if site is None: # tiles are spatial extent for t in tiles: extents.append(cls(dataclass, tiles=[t], pcov=pcov, ptile=ptile)) else: features = open_vector(site, key, where) for f in features: extents.append(cls(dataclass, feature=f, tiles=tiles, pcov=pcov, ptile=ptile)) return extents
def tile_bounds(cls, tile): """ Get the bounds of the tile (in same units as tiles vector) """ # vector = open_vector(cls.get_setting('tiles')) # extent = vector.where('tileid==%s' % tile).extent() # return [extent.x0(), extent.y0(), extent.x1(), extent.y1()] vector = open_vector(cls.get_setting('tiles')) features = vector.where('tileid', tile) if len(features) != 1: raise Exception('there should be a single tile with id %s' % tile) # set_trace() extent = features[0].Extent() return [extent.x0(), extent.y0(), extent.x1(), extent.y1()]
def query_service(cls, asset, tile, date): if asset == _cdlmkii: return None url = "https://nassgeodata.gmu.edu/axis2/services/CDLService/GetCDLFile" tile_vector = utils.open_vector(cls.Repository.get_setting('tiles'), 'STATE_ABBR')[tile] params = { 'year': date.year, 'fips': tile_vector['STATE_FIPS'] } xml = requests.get(url, params=params, verify=False) if xml.status_code != 200: return None root = ElementTree.fromstring(xml.text) file_url = root.find('./returnURL').text return {'url': file_url, 'basename': "{}_{}_cdl_cdl.tif".format(tile, date.year)}
def factory(cls, dataclass, site=None, key='', where='', tiles=None, pcov=0.0, ptile=0.0): """ Create array of SpatialExtent instances """ if site is None and tiles is None: # raise Exception('Site geometry and/or tile ids required') # TODO: make this better. It only returns the tiles you have without telling # you that is what it is doing. tiles = dataclass.Asset.Repository.find_tiles() extents = [] if site is None: # tiles are spatial extent for t in tiles: extents.append(cls(dataclass, tiles=[t], pcov=pcov, ptile=ptile)) else: features = open_vector(site, key, where) for f in features: extents.append(cls(dataclass, feature=f, tiles=tiles, pcov=pcov, ptile=ptile)) return extents
def vector2tiles(cls, vector, pcov=0.0, ptile=0.0, tilelist=None): """ Return matching tiles and coverage % for provided vector """ from osgeo import ogr, osr # open tiles vector v = open_vector(cls.get_setting('tiles')) shp = ogr.Open(v.Filename()) if v.LayerName() == '': layer = shp.GetLayer(0) else: layer = shp.GetLayer(v.LayerName()) # create and warp site geometry ogrgeom = ogr.CreateGeometryFromWkt(vector.WKT()) srs = osr.SpatialReference(vector.Projection()) trans = osr.CoordinateTransformation(srs, layer.GetSpatialRef()) ogrgeom.Transform(trans) # convert to shapely geom = loads(ogrgeom.ExportToWkt()) # find overlapping tiles tiles = {} layer.SetSpatialFilter(ogrgeom) layer.ResetReading() feat = layer.GetNextFeature() while feat is not None: tgeom = loads(feat.GetGeometryRef().ExportToWkt()) if tgeom.overlaps(geom): area = geom.intersection(tgeom).area if area != 0: tile = cls.feature2tile(feat) tiles[tile] = (area / geom.area, area / tgeom.area) feat = layer.GetNextFeature() # remove any tiles not in tilelist or that do not meet thresholds for % cover remove_tiles = [] if tilelist is None: tilelist = tiles.keys() for t in tiles: if (tiles[t][0] < (pcov / 100.0)) or (tiles[t][1] < (ptile / 100.0)) or t not in tilelist: remove_tiles.append(t) for t in remove_tiles: tiles.pop(t, None) return tiles
def vector2tiles(cls, vector, pcov=0.0, ptile=0.0, tilelist=None): """ Return matching tiles and coverage % for provided vector """ from osgeo import ogr, osr # open tiles vector v = open_vector(cls.get_setting('tiles')) shp = ogr.Open(v.Filename()) if v.LayerName() == '': layer = shp.GetLayer(0) else: layer = shp.GetLayer(v.LayerName()) # create and warp site geometry ogrgeom = ogr.CreateGeometryFromWkt(vector.WKT()) srs = osr.SpatialReference(vector.Projection()) trans = osr.CoordinateTransformation(srs, layer.GetSpatialRef()) ogrgeom.Transform(trans) # convert to shapely geom = loads(ogrgeom.ExportToWkt()) # find overlapping tiles tiles = {} layer.SetSpatialFilter(ogrgeom) layer.ResetReading() feat = layer.GetNextFeature() while feat is not None: tgeom = loads(feat.GetGeometryRef().ExportToWkt()) if tgeom.intersects(geom): area = geom.intersection(tgeom).area if area != 0: tile = cls.feature2tile(feat) tiles[tile] = (area / geom.area, area / tgeom.area) feat = layer.GetNextFeature() # remove any tiles not in tilelist or that do not meet thresholds for % cover remove_tiles = [] if tilelist is None: tilelist = tiles.keys() for t in tiles: if (tiles[t][0] < (pcov / 100.0)) or (tiles[t][1] < (ptile / 100.0)) or t not in tilelist: remove_tiles.append(t) for t in remove_tiles: tiles.pop(t, None) return tiles
def tile_bounds(cls, tile): """ Get the bounds of the tile (in same units as tiles vector) """ vector = open_vector(cls.get_setting('tiles')) extent = vector.where('tileid==%s' % tile).extent() return [extent.x0(), extent.y0(), extent.x1(), extent.y1()]
from gips.utils import open_vector from shapely.wkt import loads from timeit import Timer extent = SpatialExtent.factory(data, 'Belknap.shp') assert len(extent[0].tiles) == 1, ('Belknap is contained wholly NH tile, ' 'but vector2tiles says it is in {} tiles' .format(len(extent[0].tiles))) t = 'NH.shp' f = 'NHseacoast.shp' aWKT = open_vector(t)[0].WKT() bWKT = open_vector(f)[0].WKT() a = loads(aWKT) b = loads(bWKT) iters = 1000 def intersect(): global a, b return a.intersects(b) def overlap(): global a, b return a.overlaps(b)