def test_contour_too_many_levels(): ogr_ds = ogr.GetDriverByName('ESRI Shapefile').CreateDataSource( '/vsimem/contour.shp') ogr_lyr = ogr_ds.CreateLayer('contour', geom_type=ogr.wkbLineString) field_defn = ogr.FieldDefn('ID', ogr.OFTInteger) ogr_lyr.CreateField(field_defn) content1 = """ncols 2 nrows 2 xllcorner 0 yllcorner 0 cellsize 1 1e30 0 0 0""" content2 = """ncols 2 nrows 2 xllcorner 0 yllcorner 0 cellsize 1 1e6 0 0 0""" for content in (content1, content2): with gdaltest.tempfile('/vsimem/test.asc', content): ds = gdal.Open('/vsimem/test.asc') with gdaltest.error_handler(): assert gdal.ContourGenerateEx( ds.GetRasterBand(1), ogr_lyr, options=["LEVEL_INTERVAL=1", "ID_FIELD=0"]) != 0 with gdaltest.tempfile('/vsimem/test.asc', content): ds = gdal.Open('/vsimem/test.asc') with gdaltest.error_handler(): assert gdal.ContourGenerateEx( ds.GetRasterBand(1), ogr_lyr, options=[ "LEVEL_INTERVAL=1", "LEVEL_EXP_BASE=1.0001", "ID_FIELD=0" ]) != 0 ogr_ds = None ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource( '/vsimem/contour.shp')
def test_contour_raster_acquisition_error(): ogr_ds = ogr.GetDriverByName('Memory').CreateDataSource('') ogr_lyr = ogr_ds.CreateLayer('contour', geom_type=ogr.wkbLineString) field_defn = ogr.FieldDefn('ID', ogr.OFTInteger) ogr_lyr.CreateField(field_defn) ds = gdal.Open('../gcore/data/byte_truncated.tif') with gdaltest.error_handler(): assert gdal.ContourGenerateEx( ds.GetRasterBand(1), ogr_lyr, options=["LEVEL_INTERVAL=1", "ID_FIELD=0"]) != 0
def test_contour_nodata_precision_issue_float32(): ogr_ds = ogr.GetDriverByName('ESRI Shapefile').CreateDataSource('/vsimem/contour.shp') ogr_lyr = ogr_ds.CreateLayer('contour', geom_type=ogr.wkbLineString) field_defn = ogr.FieldDefn('ID', ogr.OFTInteger) ogr_lyr.CreateField(field_defn) ds = gdal.Open('data/nodata_precision_issue_float32.tif') gdal.ContourGenerateEx(ds.GetRasterBand(1), ogr_lyr, options = [ "LEVEL_INTERVAL=0.1", "ID_FIELD=0", "NODATA=%.19g" % ds.GetRasterBand(1).GetNoDataValue()] ) ds = None assert ogr_lyr.GetFeatureCount() == 0 ogr_ds = None ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('/vsimem/contour.shp')
def test_contour_3(): try: os.remove('tmp/contour.shp') except OSError: pass try: os.remove('tmp/contour.dbf') except OSError: pass try: os.remove('tmp/contour.shx') except OSError: pass ogr_ds = ogr.GetDriverByName('ESRI Shapefile').CreateDataSource( 'tmp/contour.shp') ogr_lyr = ogr_ds.CreateLayer('contour', geom_type=ogr.wkbMultiPolygon) field_defn = ogr.FieldDefn('ID', ogr.OFTInteger) ogr_lyr.CreateField(field_defn) field_defn = ogr.FieldDefn('elevMin', ogr.OFTReal) ogr_lyr.CreateField(field_defn) field_defn = ogr.FieldDefn('elevMax', ogr.OFTReal) ogr_lyr.CreateField(field_defn) ds = gdal.Open('tmp/gdal_contour.tif') #gdal.ContourGenerateEx(ds.GetRasterBand(1), 0, 0, 0, [10, 20, 25], 0, 0, ogr_lyr, 0, 1, 1) gdal.ContourGenerateEx(ds.GetRasterBand(1), ogr_lyr, options=[ "FIXED_LEVELS=10,20,25", "ID_FIELD=0", "ELEV_FIELD_MIN=1", "ELEV_FIELD_MAX=2", "POLYGONIZE=TRUE" ]) ds = None size = 160 precision = 1. / size expected_envelopes = [[1.0, 2.0, 49.0, 50.0], [1.25, 1.75, 49.25, 49.75], [ 1.25 + 0.125, 1.75 - 0.125, 49.25 + 0.125, 49.75 - 0.125 ], [ 1.25 + 0.125 + 0.0625, 1.75 - 0.125 - 0.0625, 49.25 + 0.125 + 0.0625, 49.75 - 0.125 - 0.0625 ]] expected_height = [10, 20, 25, 10000] lyr = ogr_ds.ExecuteSQL("select * from contour order by elevMin asc") assert lyr.GetFeatureCount() == len(expected_envelopes) i = 0 feat = lyr.GetNextFeature() while feat is not None: if i < 3 and feat.GetField('elevMax') != expected_height[i]: pytest.fail('Got %f as z. Expected %f' % (feat.GetField('elevMax'), expected_height[i])) elif i > 0 and i < 3 and feat.GetField('elevMin') != expected_height[ i - 1]: pytest.fail('Got %f as z. Expected %f' % (feat.GetField('elevMin'), expected_height[i - 1])) envelope = feat.GetGeometryRef().GetEnvelope() for j in range(4): if expected_envelopes[i][j] != pytest.approx( envelope[j], abs=precision / 2 * 1.001): print('i=%d, wkt=%s' % (i, feat.GetGeometryRef().ExportToWkt())) print(feat.GetGeometryRef().GetEnvelope()) pytest.fail( '%f, %f' % (expected_envelopes[i][j] - envelope[j], precision / 2)) i = i + 1 feat = lyr.GetNextFeature() ogr_ds.ReleaseResultSet(lyr) ogr_ds.Destroy()