def test_countFeatures(): # Simple vector count assert vector.countFeatures(MULTI_FTR_SHAPE_PATH) == 4 # same SRS, geom filter cnt = vector.countFeatures(MULTI_FTR_SHAPE_PATH, geom=geom.box(5.89, 48.77, 6.89, 49.64, srs=EPSG4326)) assert cnt == 2 # different SRS, geom filter cnt = vector.countFeatures(MULTI_FTR_SHAPE_PATH, geom=geom.box(4022802, 2867575, 4104365, 2938843, srs=EPSG3035)) assert cnt == 2 # where filter cnt = vector.countFeatures(MULTI_FTR_SHAPE_PATH, where="name LIKE 'mo%'") assert cnt == 2
def test_countFeatures(): if countFeatures(MULTI_FTR_SHAPE_PATH) != 4: error("Simple vector count") if countFeatures(MULTI_FTR_SHAPE_PATH, geom=box(5.89, 48.77, 6.89, 49.64, srs=EPSG4326)) != 2: error("Vector count - same SRS, geom filter") if countFeatures(MULTI_FTR_SHAPE_PATH, geom=box(4022802, 2867575, 4104365, 2938843, srs=EPSG3035)) != 2: error("Vector count - different SRS, geom filter") if countFeatures(MULTI_FTR_SHAPE_PATH, where="name LIKE 'mo%'") != 2: error("Vector count - where filter") print("countFeatures passed")
def test_warp(): # Change resolution to disk d = raster.warp(CLC_RASTER_PATH, pixelHeight=200, pixelWidth=200, output=result("warp1.tif")) v1 = raster.extractMatrix(d) assert np.isclose(v1.mean(), 16.3141463057) # change resolution to memory d = raster.warp(CLC_RASTER_PATH, pixelHeight=200, pixelWidth=200) v2 = raster.extractMatrix(d) assert np.isclose(v1, v2).all() # Do a cutline from disk d = raster.warp(CLC_RASTER_PATH, cutline=AACHEN_SHAPE_PATH, output=result("warp3.tif"), noData=99) v3 = raster.extractMatrix(d) assert np.isclose(v3.mean(), 89.9568135904) assert np.isclose(v3[0, 0], 99) # Do a cutline from memory d = raster.warp(CLC_RASTER_PATH, cutline=geom.box(*AACHEN_SHAPE_EXTENT_3035, srs=EPSG3035), noData=99) v4 = raster.extractMatrix(d) assert np.isclose(v4[0, 0], 99) assert np.isclose(v4.mean(), 76.72702479) # Do a flipped-source check d = raster.warp(CLC_FLIPCHECK_PATH, cutline=geom.box(*AACHEN_SHAPE_EXTENT_3035, srs=EPSG3035), noData=99) v5 = raster.extractMatrix(d) assert np.isclose(v4, v5).all() d = raster.warp(CLC_FLIPCHECK_PATH, pixelHeight=200, pixelWidth=200, output=result("warp6.tif")) v6 = raster.extractMatrix(d) assert np.isclose(v1, v6).all()
def test_extractFeatures(): # test basic vi = list(vector.extractFeatures(BOXES, asPandas=False)) assert len(vi) == 3 # count mismatch assert (vi[0][0].Area() == 1.0) # geom mismatch assert (vi[0][1]['name'] == "harry") # attribute mismatch assert (vi[1][0].Area() == 4.0) # geom mismatch assert (vi[1][1]['name'] == "ron") # attribute mismatch assert (vi[2][0].Area() == 9.0) # geom mismatch assert (vi[2][1]['name'] == "hermoine") # attribute mismatch # test clip vi = list( vector.extractFeatures(BOXES, geom=geom.box(0, 0, 3, 3, srs=EPSG4326), asPandas=False)) assert len(vi) == 2 # count mismatch assert (vi[0][0].Area() == 1.0) # geom mismatch assert (vi[0][1]['name'] == "harry") # attribute mismatch assert (vi[1][0].Area() == 4.0) # geom mismatch assert (vi[1][1]['name'] == "ron") # attribute mismatch # test srs change and attribute filter vi = list( vector.extractFeatures(BOXES, where="smart>0", srs=EPSG3035, asPandas=False)) assert len(vi) == 1 # count mismatch assert vi[0][0].GetSpatialReference().IsSame(EPSG3035) # srs mismatch assert (vi[0][1]['name'] == "hermoine") # attribute mismatch # Test loading as a dataframe vi = vector.extractFeatures(BOXES, asPandas=True) assert vi.shape == (3, 3) # shape mismatch assert (vi.geom[0].Area() == 1.0) # geom mismatch assert (vi['name'][0] == "harry") # attribute mismatch assert (vi.geom[1].Area() == 4.0) # geom mismatch assert (vi['name'][1] == "ron") # attribute mismatch assert (vi.geom[2].Area() == 9.0) # geom mismatch assert (vi['name'][2] == "hermoine") # attribute mismatch
def test_flatten(): # Overlapping polygons bounds = [(i, i, i + 2, i + 2) for i in range(5)] # test basic combination geomList = [geom.box(b, srs=EPSG4326) for b in bounds] f1 = geom.flatten(geomList) assert np.isclose(f1.Area(), 16.0) # flattened area env = f1.GetEnvelope() assert np.isclose(env[0], 0) assert np.isclose(env[1], 6) assert np.isclose(env[2], 0) assert np.isclose(env[3], 6) assert f1.GetSpatialReference().IsSame(EPSG4326) # flattened srs
def test_extractFeatures(): # test basic vi = list(extractFeatures(BOXES, asPandas=False)) if len(vi) != 3: error("extractFeatures 1 - count mismatch") if (vi[0][0].Area() != 1.0): error("extractFeatures 1 - geom mismatch") if (vi[0][1]['name'] != "harry"): error("extractFeatures 1 - attribute mismatch") if (vi[1][0].Area() != 4.0): error("extractFeatures 1 - geom mismatch") if (vi[1][1]['name'] != "ron"): error("extractFeatures 1 - attribute mismatch") if (vi[2][0].Area() != 9.0): error("extractFeatures 1 - geom mismatch") if (vi[2][1]['name'] != "hermoine"): error("extractFeatures 1 - attribute mismatch") # test clip vi = list( extractFeatures(BOXES, geom=box(0, 0, 3, 3, srs=EPSG4326), asPandas=False)) if len(vi) != 2: error("extractFeatures 2 - count mismatch") if (vi[0][0].Area() != 1.0): error("extractFeatures 2 - geom mismatch") if (vi[0][1]['name'] != "harry"): error("extractFeatures 2 - attribute mismatch") if (vi[1][0].Area() != 4.0): error("extractFeatures 2 - geom mismatch") if (vi[1][1]['name'] != "ron"): error("extractFeatures 2 - attribute mismatch") # test srs change and attribute filter vi = list( extractFeatures(BOXES, where="smart>0", srs=EPSG3035, asPandas=False)) if len(vi) != 1: error("extractFeatures 3 - count mismatch") if (not vi[0][0].GetSpatialReference().IsSame(EPSG3035)): error("extractFeatures 3 - srs mismatch") if (vi[0][1]['name'] != "hermoine"): error("extractFeatures 3 - attribute mismatch") # Test loading as a dataframe vi = extractFeatures(BOXES, asPandas=True) if vi.shape != (3, 3): error("extractFeatures 4 - shape mismatch") if (vi.geom[0].Area() != 1.0): error("extractFeatures 4 - geom mismatch") if (vi['name'][0] != "harry"): error("extractFeatures 4 - attribute mismatch") if (vi.geom[1].Area() != 4.0): error("extractFeatures 4 - geom mismatch") if (vi['name'][1] != "ron"): error("extractFeatures 4 - attribute mismatch") if (vi.geom[2].Area() != 9.0): error("extractFeatures 4 - geom mismatch") if (vi['name'][2] != "hermoine"): error("extractFeatures 4 - attribute mismatch") print("extractFeatures passed")
def test_mutateVector(): # Setup ext_small = (6.1, 50.7, 6.25, 50.9) box_aachen = box(AACHEN_SHAPE_EXTENT, srs=EPSG4326) box_aachen.TransformTo(EPSG3035) sentance = [ "Never", "have", "I", "ever", "ridden", "on", "a", "horse", "Did", "you", "know", "that", "?" ] sentanceSmall = ["Never", "have", "I", "ever", "you"] ## simple repeater ps1 = mutateVector(AACHEN_POINTS, processor=None) res1 = extractFeatures(ps1) if res1.shape[0] != 13: error("mutateVector 1 - item count") for i in range(13): if not res1.geom[i].GetSpatialReference().IsSame(EPSG4326): error("mutateVector 1 - geom srs") if not res1.geom[i].GetGeometryName() == "POINT": error("mutateVector 1 - geom type") if res1['word'][i] != sentance[i]: error("mutateVector 1 - attribute writing") ## spatial filtering ps2 = mutateVector(AACHEN_POINTS, processor=None, geom=ext_small) res2 = extractFeatures(ps2) if res2.shape[0] != 5: error("mutateVector 2 - item count") for i in range(5): if not (res2['word'][i] == sentanceSmall[i]): error("mutateVector 2 - attribute writing") ## attribute and spatial filtering ps3 = mutateVector(AACHEN_POINTS, processor=None, geom=ext_small, where="id<5") res3 = extractFeatures(ps3) if res3.shape[0] != 4: error("mutateVector 3 - item count") for i in range(4): if not (res3['word'][i] == sentanceSmall[i]): error("mutateVector 3 - attribute writing") ## Test no items found ps4 = mutateVector(AACHEN_POINTS, processor=None, where="id<0") if not ps4 is None: error("mutateVector 4 - no items found") ## Simple grower func ina new srs def growByWordLength(ftr): size = len(ftr["word"]) * 1000 newGeom = ftr.geom.Buffer(size) return {'geom': newGeom, "size": size} output5 = result("mutateVector5.shp") mutateVector(AACHEN_POINTS, processor=growByWordLength, srs=EPSG3035, output=output5, overwrite=True) ps5 = loadVector(output5) res5 = extractFeatures(ps5) if res5.shape[0] != 13: error("mutateVector 5 - item count") for i in range(13): if not res5.geom[i].GetSpatialReference().IsSame(EPSG3035): error("mutateVector 5 - geom srs") if not res5.geom[i].GetGeometryName() == "POLYGON": error("mutateVector 5 - geom type") if not (res5['word'][i] == sentance[i]): error("mutateVector 5 - attribute writing") if not (res5['size'][i] == len(sentance[i]) * 1000): error("mutateVector 5 - attribute writing") # test if the new areas are close to what they shoud be area = 1000 * len(sentance[i]) * 1000 * len(sentance[i]) * np.pi if not abs(1 - area / res5.geom[i].Area()) < 0.001: error("mutateVector 5 - geom area") ## Test inline processor, with filtering, and writign to file mutateVector(AACHEN_ZONES, srs=4326, geom=box_aachen, where="YEAR>2000", processor=lambda ftr: { 'geom': ftr.geom.Centroid(), "YEAR": ftr["YEAR"] }, output=result("mutateVector6.shp"), overwrite=True) print("mutateVector passed")
def test_mutateVector(): # Setup ext_small = (6.1, 50.7, 6.25, 50.9) box_aachen = geom.box(AACHEN_SHAPE_EXTENT, srs=EPSG4326) box_aachen.TransformTo(EPSG3035) sentance = [ "Never", "have", "I", "ever", "ridden", "on", "a", "horse", "Did", "you", "know", "that", "?" ] sentanceSmall = ["Never", "have", "I", "ever", "you"] # simple repeater ps1 = vector.mutateVector(AACHEN_POINTS, processor=None) res1 = vector.extractFeatures(ps1) assert res1.shape[0] == 13 # item count") for i in range(13): assert res1.geom[i].GetSpatialReference().IsSame(EPSG4326) # geom srs assert res1.geom[i].GetGeometryName() == "POINT" # geom type assert res1['word'][i] == sentance[i] # attribute writing # spatial filtering ps2 = vector.mutateVector(AACHEN_POINTS, processor=None, geom=ext_small) res2 = vector.extractFeatures(ps2) assert res2.shape[0] == 5 # item count for i in range(5): assert (res2['word'][i] == sentanceSmall[i]) # attribute writing # attribute and spatial filtering ps3 = vector.mutateVector(AACHEN_POINTS, processor=None, geom=ext_small, where="id<5") res3 = vector.extractFeatures(ps3) assert res3.shape[0] == 4 # item count for i in range(4): assert (res3['word'][i] == sentanceSmall[i]) # attribute writing # Test no items found ps4 = vector.mutateVector(AACHEN_POINTS, processor=None, where="id<0") assert ps4 is None # no items found # Simple grower func in a new srs def growByWordLength(ftr): size = len(ftr["word"]) * 10 newGeom = ftr.geom.Buffer(size) return {'geom': newGeom, "size": size} output5 = result("mutateVector5.shp") vector.mutateVector(AACHEN_POINTS, processor=growByWordLength, srs=EPSG3035, output=output5, overwrite=True) ps5 = vector.loadVector(output5) res5 = vector.extractFeatures(ps5) assert res5.shape[0] == 13 # item count for i in range(13): assert res5.geom[i].GetSpatialReference().IsSame(EPSG3035) # geom srs assert res5.geom[i].GetGeometryName() == "POLYGON" # geom type assert (res5['word'][i] == sentance[i]) # attribute writing assert (res5['size'][i] == len(sentance[i]) * 10) # attribute writing # test if the new areas are close to what they shoud be area = np.power(10 * len(sentance[i]), 2) * np.pi assert np.isclose(area, res5.geom[i].Area(), rtol=1.e-3) # geom area # Test inline processor, with filtering, and writing to file vector.mutateVector(AACHEN_ZONES, srs=4326, geom=box_aachen, where="YEAR>2000", processor=lambda ftr: { 'geom': ftr.geom.Centroid(), "YEAR": ftr["YEAR"] }, output=result("mutateVector6.shp"), overwrite=True)
def test_box(): # fun func b1 = geom.box(0, 0, 5, 10, srs=EPSG3035) assert np.isclose(b1.Area(), 50)