def gdal_warp(inRasterPath, outRasterPath, OutPutProjection=3035 , delete_orig = False): print("Warp: %s" % inRasterPath) # Open source dataset src_ds = gdal.Open(inRasterPath) # Define target SRS dst_srs = osr.SpatialReference() dst_srs.ImportFromEPSG(OutPutProjection) dst_wkt = dst_srs.ExportToWkt() error_threshold = 0.125 # error threshold --> use same value as in gdalwarp resampling = gdal.GRA_Bilinear # Call AutoCreateWarpedVRT() to fetch default values for target raster dimensions and geotransform tmp_ds = gdal.AutoCreateWarpedVRT( src_ds, None, # src_wkt : left to default value --> will use the one from source dst_wkt, resampling, error_threshold ) # Create the final warped raster dst_ds = gdal.GetDriverByName('GTiff').CreateCopy(outRasterPath, tmp_ds) dst_ds = None print(" Done: %s" % outRasterPath) try: pass #os.remove(inRasterPath) except Exception as e: print(e) print("cannot remove: %s" % inRasterPath) return
def warp_reproject(infile, outfile, t_epsg=4326, r='near'): # Define target SRS dst_srs = osr.SpatialReference() dst_srs.ImportFromEPSG(t_epsg) dst_wkt = dst_srs.ExportToWkt() # error threshold error_threshold = 0.125 # error threshold --> use same value as in gdalwarp # resampling if r == 'near': resampling = gdal.GRA_NearestNeighbour elif r == 'bilinear': resampling = gdal.GRA_Bilinear else: raise ValueError('Resampling `r` should be \'near\' or \'bilinear\'.') with gdal_open(infile) as src_ds: # Call AutoCreateWarpedVRT() to fetch default values for target raster dimensions and geotransform tmp_ds = gdal.AutoCreateWarpedVRT( src_ds, None, # src_wkt dst_wkt, resampling, error_threshold) # Create the final warped raster if outfile.lower().endswith('.vrt'): driver = 'VRT' else: driver = 'GTiff' dst_ds = gdal.GetDriverByName(driver).CreateCopy(outfile, tmp_ds) dst_ds = None check_gdal_success(outfile, 'gdalwarp')
def reproject_MODIS(input_name, output_name, epsg_to): ''' Reproject the merged data file by using gdalwarp. The input projection must be the MODIS projection. The output projection can be defined by the user. Keywords arguments: input_name -- 'C:/file/to/path/file.tif' string that defines the input tiff file epsg_to -- integer The EPSG code of the output dataset ''' src_ds = gdal.Open(input_name) # Define target SRS dst_srs = osr.SpatialReference() dst_srs.ImportFromEPSG(int(epsg_to)) dst_wkt = dst_srs.ExportToWkt() error_threshold = 0.125 # error threshold --> use same value as in gdalwarp resampling = gdal.GRA_NearestNeighbour # Call AutoCreateWarpedVRT() to fetch default values for target raster dimensions and geotransform tmp_ds = gdal.AutoCreateWarpedVRT( src_ds, None, # src_wkt : left to default value --> will use the one from source dst_wkt, resampling, error_threshold) dst_ds = gdal.GetDriverByName('GTiff').CreateCopy(output_name, tmp_ds) dst_ds = None return ()
def raster_import(infile, outfile, *args, **kwargs): if os.path.exists(outfile): raise FileExists options = get_kwarg('options', kwargs, ['TILED=YES']) sr = osr.SpatialReference() sr.ImportFromEPSG(3857) t_srs_prj = sr.ExportToWkt() build_overviews = get_kwarg('build_overviews', kwargs, True) geotiff = gdal.GetDriverByName("GTiff") if geotiff is None: raise RuntimeError indata = gdal.Open(infile) if indata is None: raise NoDataSourceFound if indata.GetProjectionRef() is None: indata.SetProjection(t_srs_prj) vrt = gdal.AutoCreateWarpedVRT(indata, None, t_srs_prj, 0, .125) outdata = geotiff.CreateCopy(outfile, vrt, 0, options) if build_overviews: outdata.BuildOverviews("AVERAGE") return outfile
def gdal_reproject(src, dst, epsg=3857, error_threshold=0.125, resampling=gdal.GRA_NearestNeighbour): """ Reproject a raster image :param src: The source image :param dst: The filepath/name of the output image :param epsg: The EPSG code to reproject to :param error_threshold: Default is 0.125 (same as gdalwarp commandline) :param resampling: Default method is Nearest Neighbor """ # Open source dataset src_ds = get_dataset(src) # Define target SRS dst_srs = osr.SpatialReference() dst_srs.ImportFromEPSG(int(epsg)) dst_wkt = dst_srs.ExportToWkt() # Resampling might be passed as a string if not isinstance(resampling, int): resampling = getattr(gdal, resampling) # Call AutoCreateWarpedVRT() to fetch default values # for target raster dimensions and geotransform reprojected_ds = gdal.AutoCreateWarpedVRT(src_ds, None, dst_wkt, resampling, error_threshold) # Create the final warped raster if dst: gdal.GetDriverByName('GTiff').CreateCopy(dst, reprojected_ds) return reprojected_ds
def raster2tiles(input_raster, min_zoom=None, max_zoom=None): if not _check_raster_format(input_raster): raise RuntimeError( 'Invalid raster format. Only rasters with 1, 3 or 4 bands are alowed!' ) input_srs = _get_raster_srs(input_raster) out_srs = _get_srs_from_epsg_code(3857) # mercator # TODO: out_raster ta sendo usado pra algo? if input_srs.ExportToProj4() != out_srs.ExportToProj4(): out_raster = gdal.AutoCreateWarpedVRT(input_raster, input_srs.ExportToWkt(), out_srs.ExportToWkt()) else: out_raster = input_raster out_geotransform = out_raster.GetGeoTransform() if not _check_out_geotransform(out_geotransform): raise RuntimeError( 'Georeference of the raster contains rotation or skew.') # minx, maxx, miny, maxy out_bounds = BoundingBox( out_geotransform[0], out_geotransform[0] + out_raster.RasterXSize * out_geotransform[1], out_geotransform[3] - out_raster.RasterYSize * out_geotransform[1], out_geotransform[3]) tile_size = 256 # get minimal zoom level (out_geotranform[1] == raster pixel width) max_raster = max(out_raster.RasterXSize, out_raster.RasterYSize) min_zoom_level = min_zoom or _zoom_for_pixel_size( out_geotransform[1] * max_raster / float(tile_size)) max_zoom_level = max_zoom or _zoom_for_pixel_size(out_geotransform[1]) tiles_min_max_coordinates = {} for zoom in range(min_zoom_level, max_zoom_level + 1): tminx, tminy = _meters2tile(out_bounds.minx, out_bounds.miny, zoom, tile_size=256) tmaxx, tmaxy = _meters2tile(out_bounds.maxx, out_bounds.maxy, zoom, tile_size=256) # crop tiles extending world limits (+-180,+-90) tminx, tminy = max(0, tminx), max(0, tminy) tmaxx, tmaxy = min(2**zoom - 1, tmaxx), min(2**zoom - 1, tmaxy) tiles_min_max_coordinates[zoom] = BoundingBox(tminx, tmaxx, tminy, tmaxy) for zoom in range(min_zoom_level, max_zoom_level + 1): # TODO: colocar isso no for de cima for (ty, tx), data in _generate_tile(out_raster, zoom, tiles_min_max_coordinates[zoom], tile_size=256): yield (zoom, ty, tx), data
def gdal_warp(src_ds, dst_wkt): """ GDAL warp but in python Args: src_ds: Source dataset dst_wkt: Target SRS WKT Returns: dst_ds: The warped dataset http://gis.stackexchange.com/a/140053 """ error_threshold = 0.125 # error threshold --> use same value as in gdalwarp resampling = gdal.GRA_NearestNeighbour # Call AutoCreateWarpedVRT() to fetch default values for target # raster dimensions and geotransform tmp_ds = gdal.AutoCreateWarpedVRT( src_ds, None, # src_wkt from source dst_wkt, resampling, error_threshold) dst_ds = gdal.GetDriverByName('MEM').CreateCopy('', tmp_ds) return dst_ds
def raster_to_WSG_and_UTM(raster_path, lat, lon): raster = gdal.Open(raster_path) source_projection_wkt = raster.GetProjection() inSRS_converter = osr.SpatialReference() inSRS_converter.ImportFromProj4(get_projected_coordinate_system(lat, lon)) target_projection_wkt = inSRS_converter.ExportToWkt() new_raster = gdal.AutoCreateWarpedVRT(raster, source_projection_wkt, target_projection_wkt, gdal.GRA_NearestNeighbour) return new_raster
def raster_import(infile, outfile, *args, **kwargs): if os.path.exists(outfile): raise FileExists options = get_kwarg('options', kwargs, ['TILED=YES', 'COMPRESS=LZW', 'NUM_THREADS=4']) gdal.SetCacheMax = 524288000 sr = osr.SpatialReference() sr.ImportFromEPSG(3857) t_srs_prj = sr.ExportToWkt() build_overviews = get_kwarg('build_overviews', kwargs, True) # gdal.WarpOptions( # multithread=True, # warpMemoryLimit=524288000 # ) geotiff = gdal.GetDriverByName("GTiff") if geotiff is None: raise RuntimeError indata = gdal.Open(infile) if indata is None: raise NoDataSourceFound if indata.GetProjectionRef() is None: indata.SetProjection(t_srs_prj) vrt = gdal.AutoCreateWarpedVRT(indata, None, t_srs_prj, 0, .125) outdata = geotiff.CreateCopy(outfile, vrt, 0, options) outdata = None indata = None if build_overviews: overviews_levels = get_kwarg('overviews_levels', kwargs, [2, 4, 8, 16, 32]) overviews_resampling = get_kwarg('overviews_resampling', kwargs, 'AVERAGE') overviews_options = get_kwarg('overviews_options', kwargs, ['COMPRESS_OVERVIEW=LZW']) for opt in overviews_options: key, value = opt.split('=') gdal.SetConfigOption(key, value) ds = gdal.Open(outfile) ds.BuildOverviews(overviews_resampling, overviews_levels) ds = None return outfile
def test_update_warped_vrt_xml(self): dataset = gdal.Open('NETCDF:"%s":UMass_AES' % self.test_file_arctic) warped_dataset = gdal.AutoCreateWarpedVRT(dataset, None, str(self.nsr_wkt), 0) warped_vrt = VRT.copy_dataset(warped_dataset) x_size = 100 y_size = 200 geo_transform = (0.0, 1.0, 0.0, 200.0, 0.0, -1.0) block_size = 64 working_data_type = 'Float32' warped_vrt._update_warped_vrt_xml(x_size, y_size, geo_transform, block_size, working_data_type) self.assertEqual(warped_vrt.dataset.RasterXSize, x_size) self.assertEqual(warped_vrt.dataset.RasterYSize, y_size) self.assertEqual(warped_vrt.dataset.GetGeoTransform(), geo_transform) self.assertEqual(warped_vrt.dataset.GetRasterBand(1).GetBlockSize(), [block_size, block_size]) self.assertIn('<WorkingDataType>Float32</WorkingDataType>', warped_vrt.xml)
def importDemAsArray(path, xsize=None, ysize=None, xoff=0, yoff=0): """ Программа открывает файл DEM формата GeoTIFF и считывает с него геоинформацию и матрицу высот. Входной файл должен быть записан в системе координат WGS-84 (самы распространенный формат, позже реализуем для любого). Входной dataset преобразуется в систему координат EPSG:3395 - Pseudo Mercator (прямоугольная система с учетом кривизны Земли в метрах) xoff, yoff - начальные индексы среза массива, xsize, ysize - размеры массива Осторожнее с большими файлами - может зависнуть. Для подобных файлов используйте дополнительные входные параметры (пример 2). """ dataset = gdal.Open(path) # Задаем старую и новую системы координат inputEPSG = 4326 # WGS-84 outputEPSG = 3395 # World Mercator (units meters) inSpatialRef = osr.SpatialReference() inSpatialRef.ImportFromEPSG(inputEPSG) outSpatialRef = osr.SpatialReference() outSpatialRef.ImportFromEPSG(outputEPSG) newProjection = outSpatialRef.ExportToWkt() # Задаем новый объект на базе dataset с новой системой координат warped = gdal.AutoCreateWarpedVRT(dataset, dataset.GetProjection(), newProjection) gt = warped.GetGeoTransform() # Чтение высот как матрицы Z = dataset.ReadAsArray(xoff, yoff, xsize, ysize) # определяем массивы координат x и y x = np.linspace(0, Z.shape[1] - 1, Z.shape[1]) y = np.linspace(0, Z.shape[0] - 1, Z.shape[0]) # Делаем meshgrid: X,Y X, Y = np.meshgrid(x, y) # Приведение Z к нулю в центре Z = Z - Z[Z.shape[0] / 2, Z.shape[1] / 2] # Преобразование X и Y с помощью geotransform X, Y = gt[0] + gt[1] * X + gt[2] * Y, gt[3] + gt[4] * X + gt[5] * Y # Центрирование координат X и Y # X, Y = X - X[X.shape[0] / 2, X.shape[1] / 2], Y - Y[Y.shape[0] / 2, Y.shape[1] / 2] return X, Y, Z
def vrtwarp_2(): try: os.remove('tmp/warp.vrt') except: pass gcp_ds = gdal.OpenShared('data/rgb_gcp.vrt', gdal.GA_ReadOnly) gdaltest.vrtwarp_ds = gdal.AutoCreateWarpedVRT(gcp_ds) gcp_ds = None checksum = gdaltest.vrtwarp_ds.GetRasterBand(2).Checksum() expected = 21504 if checksum != expected: gdaltest.post_reason( 'Got checksum of %d instead of expected %d.' \ % (checksum, expected) ) return 'fail' return 'success'
def reproject(self, output, epgs, threshold=0.125, resampling=gdal.GRA_NearestNeighbour): ''' Creates and returns a copy of this raster in the given spatial reference. ''' if self.data_file == None: self.data_file = self._open_file() spatial_reference = osr.SpatialReference() spatial_reference.ImportFromEPSG(epgs) well_know_text = spatial_reference.ExportToWkt() tmp_ds = gdal.AutoCreateWarpedVRT( self.data_file, None, # src_wkt : left to default value --> will use the one from source well_know_text, resampling, threshold) gdal.GetDriverByName(str('GTiff')).CreateCopy(output, tmp_ds) return Data(output)
def gdal_warp(src_file_path, dst_file_path, epsg_num): # Open source dataset src_ds = gdal.Open(src_file_path) # Define target SRS dst_srs = osr.SpatialReference() dst_srs.ImportFromEPSG(epsg_num) dst_wkt = dst_srs.ExportToWkt() error_threshold = 0.125 # error threshold --> use same value as in gdalwarp resampling = gdal.GRA_NearestNeighbour # Call AutoCreateWarpedVRT() to fetch default values for target raster dimensions and geotransform tmp_ds = gdal.AutoCreateWarpedVRT( src_ds, None, # src_wkt : left to default value --> will use the one from source dst_wkt, resampling, error_threshold ) # Create the final warped raster dst_ds = gdal.GetDriverByName('GTiff').CreateCopy(dst_file_path, tmp_ds) dst_ds = None
newcoord = "-179.99" newof = os.path.dirname(src_filename) + os.sep + os.path.basename(src_filename).split(".")[0] + "_v2.tif" # Subset raster. Note subset window given in ulx, uly, lrx, lry order subprocess.call(["gdal_translate", "-co", "COMPRESS=LZW", "-projwin", newcoord, str(src_geotrans[3]), str(src_geotrans[0] + src_geotrans[1]*src.RasterXSize), str(src_geotrans[3] + src_geotrans[5]*src.RasterYSize), src_filename, newof]) # Reproject and resample layers. src = gdal.Open(newof, gdalconst.GA_ReadOnly) src_proj = src.GetProjection() src_geotrans = src.GetGeoTransform() # Get default extent of virtually projected raster # Get projection from template raster rprjobj = gdal.Open(prjrast, gdalconst.GA_ReadOnly) rprj_wkt = rprjobj.GetProjection() # Virtual VRT tfile = gdal.AutoCreateWarpedVRT(src, src_proj, rprj_wkt) dst_xsize = tfile.RasterXSize dst_ysize = tfile.RasterYSize dst_gt = tfile.GetGeoTransform() tfile = None src = None # Get extent coordinates from reprojected raster xmin = dst_gt[0] ymax = dst_gt[3] xmax = xmin + dst_gt[1]*dst_xsize ymin = ymax + dst_gt[5]*dst_ysize # Get columns and rows of new 30m dataset newxres = int((xmax - xmin) / xint) + 10 newyres = int((ymax - ymin) / yint) + 10
# Define target SRS using EPSG dst_srs = osr.SpatialReference() dst_srs.ImportFromEPSG(21096) dst_wkt = dst_srs.ExportToWkt() # apply the wkt's ds.SetGCPs(gcp_list, dst_wkt) # settings for transform error_threshold = 0.125 # error threshold #same value as in gdalwarp resampling = gdal.GRA_NearestNeighbour # warp the image to new CRS (effectively does nothing but save the new version) tmp_ds = gdal.AutoCreateWarpedVRT( ds, dst_wkt, # None # src_wkt : left to default value --> will use the one from source dst_wkt, resampling, error_threshold) # Create the final warped raster (needs to be GeoTiff for tile creation) dst_ds = gdal.GetDriverByName('GTiff').CreateCopy( "georeferenced/" + file[:-3] + "tif", tmp_ds) # clean up datasets dst_ds = None tmp_ds = None ds = None ''' Now all of the sheets are extracted and georeferenced, combine them all into a single VRT ''' # get all of the files that will be in the vrt
def clipImageJP2(extent, sourceImagePath, outputImagePath): WGS84_projection = 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]' in_srs = osr.SpatialReference() in_srs.ImportFromWkt(WGS84_projection) dataset = gdal.Open(sourceImagePath) UTM_projection = dataset.GetProjection() out_srs = osr.SpatialReference() out_srs.ImportFromWkt(UTM_projection) cols = dataset.RasterXSize rows = dataset.RasterYSize ################################################# geotransform = dataset.GetGeoTransform() ################################################# error_threshold = 0.125 resampling = gdal.GRA_NearestNeighbour dataset_middle = gdal.AutoCreateWarpedVRT(dataset, None, UTM_projection, resampling, error_threshold) cols = dataset_middle.RasterXSize rows = dataset_middle.RasterYSize ################################################# xOrigin = geotransform[0] yOrigin = geotransform[3] pixelWidth = geotransform[1] pixelHeight = geotransform[5] transform = osr.CoordinateTransformation(in_srs, out_srs) i1j1 = transform.TransformPoint(extent['minX'], extent['minY']) i2j2 = transform.TransformPoint(extent['maxX'], extent['maxY']) i1 = int((i1j1[0] - xOrigin) / pixelWidth) j1 = int((i1j1[1] - yOrigin) / pixelHeight) i2 = int((i2j2[0] - xOrigin) / pixelWidth) j2 = int((i2j2[1] - yOrigin) / pixelHeight) new_cols = i2 - i1 + 1 new_rows = j1 - j2 + 1 data = dataset.ReadAsArray(i1, j2, new_cols, new_rows) if numpy.any(data): newX = xOrigin + i1 * pixelWidth newY = yOrigin + j2 * pixelHeight new_transform = (newX, pixelWidth, 0.0, newY, 0.0, pixelHeight) dst_ds = gdal.GetDriverByName('GTiff').Create(outputImagePath, new_cols, new_rows, bands = 1, eType = gdal.GDT_Int16) dst_ds.SetProjection(UTM_projection) dst_ds.SetGeoTransform(new_transform) if data.ndim <3: dst_ds.GetRasterBand(1).WriteArray(data) dst_ds = None dataset_middle = None dataset = None else: dst_ds = None dataset_middle = None dataset = None
def netcdf_cfproj_testcopy(projTuples, origTiff, interFormats, inPath, outPath, resFilename): """Test a Geotiff file can be converted to NetCDF, and projection in CF-1 conventions can be successfully maintained. Save results to file. :arg: projTuples - list of tuples :arg: interFormats - dict of intermediate format overrides :arg: outPath - path to save output :arg: resFilename - results filename to write to. """ silent = True gdaltest.netcdf_drv_silent = True bWriteGdalTags = "YES" #silent = False gdaltest.netcdf_drv_silent = False # bWriteGdalTags="NO" result = 'success' # Test if ncdump is available try: (ret, err) = gdaltest.runexternal_out_and_err('ncdump -h') except: #nothing is supported as ncdump not found print('NOTICE: netcdf version not found') return 'skip' i = err.find('netcdf library version ') #version not found if i == -1: print('NOTICE: netcdf version not found') return 'skip' if not os.path.exists(outPath): os.makedirs(outPath) resFile = open(os.path.join(outPath, resFilename), "w") if not os.path.exists(outPath): os.makedirs(outPath) heading = "Testing GDAL translation results to NetCDF\n" resFile.write(heading) resFile.write(len(heading) * "=" + "\n") # now = datetime.datetime.now() # resFile.write("*Date/time:* %s\n" % (now.strftime("%Y-%m-%d %H:%M"))) resFile.write("\n") resPerProj = {} dsTiff = gdal.Open(os.path.join(inPath, origTiff), GA_ReadOnly) s_srs_wkt = dsTiff.GetProjection() #objects to hold the various tests i_t = 0 tst = {} tst_res = {} for proj in projTuples: try: intFmt = interFormats[proj[0]] except KeyError: intFmt = netcdf_cfproj_def_int_format intExt = netcdf_cfproj_format_fnames[intFmt] # Our little results data structures if not silent: print("") print("Testing %s (%s) translation:" % (proj[0], proj[1])) if not silent: print("About to create raster in chosen SRS") projVrt = os.path.join(outPath, "%s_%s.vrt" % \ (origTiff.rstrip('.tif'), proj[0] )) projRaster = os.path.join(outPath, "%s_%s.%s" % \ (origTiff.rstrip('.tif'), proj[0], intExt )) # projOpts = "-t_srs '%s'" % (proj[2]) # cmd = " ".join(['gdalwarp', projOpts, origTiff, projRaster]) srs = osr.SpatialReference() srs.SetFromUserInput(proj[2]) t_srs_wkt = srs.ExportToWkt() if not silent: print("going to warp file " + origTiff + "\n" + s_srs_wkt + "\ninto file " + projRaster + "\n" + t_srs_wkt) dswarp = gdal.AutoCreateWarpedVRT(dsTiff, s_srs_wkt, t_srs_wkt, GRA_NearestNeighbour, 0) drv_inter = gdal.GetDriverByName(intFmt) drv_netcdf = gdal.GetDriverByName("netcdf") dsw = drv_inter.CreateCopy(projRaster, dswarp, 0) if not silent: print("Warped %s to %s" % (proj[0], projRaster)) projNc = os.path.join(outPath, "%s_%s.nc" % \ (origTiff.rstrip('.tif'), proj[0] )) #Force GDAL tags to be written to make testing easier, with preserved datum etc ncCoOpts = "-co WRITE_GDAL_TAGS=yes" # cmd = " ".join(['gdal_translate', "-of netCDF", ncCoOpts, projRaster, # projNc]) if not silent: print("About to translate to NetCDF") dst = drv_netcdf.CreateCopy(projNc, dsw, 0, ['WRITE_GDAL_TAGS=' + bWriteGdalTags]) #For drivers like HFA, line below ESSENTIAL so that all info is # saved to new raster file - which we'll reopen later and want # to be fully updated. dsw = None dst = None if not silent: print("Translated to %s" % (projNc)) transWorked, resDetails = netcdf_cfproj_test_cf(proj, projNc) resPerProj[proj[0]] = resDetails resFile.write("%s (%s): " % (proj[0], proj[1])) if transWorked: resFile.write("OK\n") else: resFile.write("BAD\n") if 'missingProjName' in resPerProj[proj[0]]: resFile.write("\tMissing proj name '%s'\n" % \ (resPerProj[proj[0]]['missingProjName'])) for attrib in resPerProj[proj[0]]['missingAttrs']: resFile.write("\tMissing attrib '%s'\n" % (attrib)) for cVarStdName in resPerProj[proj[0]]['missingCoordVarStdNames']: resFile.write("\tMissing coord var with std name '%s'\n" \ % (cVarStdName)) if 'cfcheck_error' in resPerProj[proj[0]]: resFile.write("\tFailed cf check: %s\n" % \ (resPerProj[proj[0]]['cfcheck_error'])) # test file copy # We now copy to a new file, just to be safe projNc2 = projNc.rstrip('.nc') + '2.nc' projRaster2 = os.path.join(outPath, "%s_%s2.%s" % \ (origTiff.rstrip('.tif'), proj[0], intExt )) tst[i_t + 1] = gdaltest.GDALTest('NETCDF', '../' + projRaster, 1, None) tst_res[i_t + 1] = tst[i_t + 1].testCreateCopy(check_gt=1, check_srs=1, new_filename=projNc2, delete_copy=0, check_minmax=0, gt_epsilon=pow(10, -8)) tst[i_t + 2] = gdaltest.GDALTest(intFmt, '../' + projNc2, 1, None) tst_res[i_t + 2] = tst[i_t + 2].testCreateCopy( check_gt=1, check_srs=1, new_filename=projRaster2, delete_copy=0, check_minmax=0, gt_epsilon=pow(10, -8)) if tst_res[i_t + 1] == 'fail' or tst_res[i_t + 2] == 'fail': result = 'fail' i_t = i_t + 2 resFile.close() if not silent: print("\n" + "*" * 80) print("Saved results to file %s" % (os.path.join(outPath, resFilename))) #result = 'success' resFile = open(os.path.join(outPath, resFilename), "r") resStr = resFile.read() if resStr.find('BAD') != -1: print('\nCF projection tests failed, here is the output (stored in file %s)\n' % \ (os.path.join(outPath, resFilename))) print(resStr) result = 'fail' return result
def clipImageTiff(extent, sourceImagePath, outputImagePath): WKT_Projection = 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]' dataset = gdal.Open(sourceImagePath) cols = dataset.RasterXSize rows = dataset.RasterYSize ################################################# GCPs = dataset.GetGCPs() GCPX = [] GCPY = [] for a, val in enumerate(GCPs): GCPX.append(GCPs[a].GCPX) GCPY.append(GCPs[a].GCPY) geotransform = {'minX':min(GCPX), 'maxX':max(GCPX), 'minY':min(GCPY), 'maxY':max(GCPY)} ################################################# error_threshold = 0.125 resampling = gdal.GRA_NearestNeighbour dataset_middle = gdal.AutoCreateWarpedVRT(dataset, None, WKT_Projection, resampling, error_threshold) cols = dataset_middle.RasterXSize rows = dataset_middle.RasterYSize geotransform = [geotransform['minX'], (geotransform['maxX']-geotransform['minX'])/cols, 0, geotransform['maxY'], 0, (geotransform['maxY']-geotransform['minY'])/rows*(-1)] dataset = None c, a, b, f, d, e = dataset_middle.GetGeoTransform() def GetPixelCoords(col, row): xp = a * col + b * row + a * 0.5 + b * 0.5 + c yp = d * col + e * row + d * 0.5 + e * 0.5 + f return(xp, yp) ################################################# xOrigin = geotransform[0] yOrigin = geotransform[3] pixelWidth = geotransform[1] pixelHeight = geotransform[5] i1 = int((extent['minX'] - xOrigin) / pixelWidth) j1 = int((extent['minY'] - yOrigin) / pixelHeight) i2 = int((extent['maxX'] - xOrigin) / pixelWidth) j2 = int((extent['maxY'] - yOrigin) / pixelHeight) new_cols = i2 - i1 + 1 new_rows = j1 - j2 + 1 data = dataset_middle.ReadAsArray(i1, j2, new_cols, new_rows) ################################################# newGCPs = [] diff = i2-i1 i, j = GetPixelCoords(i2, j2) newGCPs.append(gdal.GCP(i, j, 0.0, new_cols-1, 0.0)) #BR i, j = GetPixelCoords(i1, j2) newGCPs.append(gdal.GCP(i, j, 0.0, 0.0, 0.0)) #UL i, j = GetPixelCoords(i1, j1) newGCPs.append(gdal.GCP(i, j, 0.0, 0.0, new_rows-1)) #BL i, j = GetPixelCoords(i2, j1) newGCPs.append(gdal.GCP(i, j, 0.0, new_cols-1, new_rows-1)) #UR ################################################# newX = xOrigin + i1 * pixelWidth newY = yOrigin + j2 * pixelHeight new_transform = (newX, pixelWidth, 0.0, newY, 0.0, pixelHeight) dst_ds = gdal.GetDriverByName('GTiff').Create(outputImagePath, new_cols, new_rows, bands = 1, eType = gdal.GDT_Byte) dst_ds.SetProjection(WKT_Projection) dst_ds.SetGCPs(newGCPs, WKT_Projection) dst_ds.GetRasterBand(1).WriteArray(data) dst_ds = None dataset_middle = None
tif_files_toBuild, options=vrt_options) exec('mosaic_sinu_' + vrt_files_name + '= None') # 3. Mosaic the list of MODIS geotiff files and reproject to lat/lon projection # Define target SRS dst_srs = osr.SpatialReference() dst_srs.ImportFromEPSG(4326) # WGS 84 projection dst_wkt = dst_srs.ExportToWkt() error_threshold = 0.1 # error threshold resampling = gdal.GRA_NearestNeighbour vrt_files = sorted(glob.glob('*.vrt')) for idt in range(len(vrt_files)): # Open file src_ds = gdal.Open(vrt_files[idt]) mos_file_name = '_'.join( os.path.splitext(vrt_files[idt])[0].split('_')[2:4]) # Call AutoCreateWarpedVRT() to fetch default values for target raster dimensions and geotransform tmp_ds = gdal.AutoCreateWarpedVRT(src_ds, None, dst_wkt, resampling, error_threshold) # Crop to CONUS extent and create the final warped raster dst_ds = gdal.Translate( mos_file_name + '.tif', tmp_ds, projWin=[lon_roi_min, lat_roi_max, lon_roi_max, lat_roi_min]) dst_ds = None print(mos_file_name)
def generate_tiles(self): """ Function to generate the dynamic tiles (either merging multiple web tile sources and/or extracting data from a local GIS data source """ # print('Content-Type: text/html\n') start = time.time() tz = int(self.tz) tx = int(self.tx) ty = int(self.ty) # In case of inverted y coordinate if self.invert_y: ty2 = ty else: if type(ty) is int: ty2 = (2**tz) - ty - 1 else: ty2 = ty tilefilename = os.path.join(self.cachedir, str(tz), str(tx), "%s.%s" % (ty, self.tileext)) # Tile name used if tile is cached if self.cachedir != '': if os.path.exists(tilefilename): im = Image.open(tilefilename) if major == 2: f = StringIO() elif major == 3: f = BytesIO() im.save(f, "PNG") f.seek(0) return f.read() print('Getting Raster ' + str(time.time() - start) + ' s') if self.proj == 'geo': s_srs = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs" t_srs = "+proj=latlong +datum=wgs84 +no_defs" if self.resample == 'near': self.ResampleAlg = gdal.GRA_NearestNeighbour elif self.resample == 'bilinear': self.ResampleAlg = gdal.GRA_Bilinear if self.profile == 'mercator': self.mercator = GlobalMercator() self.tileswne = self.mercator.TileLatLonBounds self.tilewsen_merc = self.mercator.TileBounds south, west, north, east = self.tileswne(tx, ty, tz) w, s, e, n = self.tilewsen_merc(tx, ty, tz) raster_url = self.url raster_url = raster_url.replace('{$x}', str(tx)) raster_url = raster_url.replace('{$y}', str(ty2)) raster_url = raster_url.replace('{$invY}', str(ty2)) raster_url = raster_url.replace('{$z}', str(tz)) try: if major == 2: f = StringIO(urllib.urlopen(raster_url).read()) elif major == 3: f = BytesIO(urlopen(raster_url).read()) im = Image.open(f).convert('RGBA') if major == 2: f = StringIO() elif major == 3: f = BytesIO() im.save(f, "PNG") f.seek(0) except: im = Image.new('RGBA', (100, 100)) if major == 2: f = StringIO() elif major == 3: f = BytesIO() im.save(f, "PNG") f.seek(0) return f.read() content = f.read() gdal.FileFromMemBuffer('/vsimem/inmem', content) src_ds = gdal.Open('/vsimem/inmem') src_srs = osr.SpatialReference() src_srs.ImportFromProj4(s_srs) src_wkt = src_srs.ExportToWkt() dst_srs = osr.SpatialReference() dst_srs.ImportFromProj4(t_srs) dst_wkt = dst_srs.ExportToWkt() nx = src_ds.RasterXSize ny = src_ds.RasterYSize nb = src_ds.RasterCount gt = [w, (e - w) / nx, 0, n, 0, (s - n) / ny] src_ds.SetGeoTransform(gt) reproj_ds = gdal.AutoCreateWarpedVRT(src_ds, src_wkt, dst_wkt, self.ResampleAlg, 0) if nb == 1: im = self.arrayToImage(reproj_ds.GetRasterBand(1).ReadAsArray()) elif nb == 3: r = self.arrayToImage(reproj_ds.GetRasterBand(1).ReadAsArray()) g = self.arrayToImage(reproj_ds.GetRasterBand(2).ReadAsArray()) b = self.arrayToImage(reproj_ds.GetRasterBand(3).ReadAsArray()) im = Image.merge("RGB", (r, g, b)) elif nb == 4: r = self.arrayToImage(reproj_ds.GetRasterBand(1).ReadAsArray()) g = self.arrayToImage(reproj_ds.GetRasterBand(2).ReadAsArray()) b = self.arrayToImage(reproj_ds.GetRasterBand(3).ReadAsArray()) a = self.arrayToImage(reproj_ds.GetRasterBand(4).ReadAsArray()) im = Image.merge("RGBA", (r, g, b, a)) else: error('Images must have 1, 3, or 4 bands') if major == 2: f = StringIO() elif major == 3: f = BytesIO() im.save(f, "PNG") print('Saving image ' + str(time.time() - start) + ' s') # If specified, save a copy of the cached image if self.cachedir != '': print('Saving copy ' + str(time.time() - start) + ' s') if not os.path.exists(os.path.dirname(tilefilename)): os.makedirs(os.path.dirname(tilefilename)) im.save(tilefilename, "PNG") gdal.Unlink('/vsimem/tiffinmem') del (src_ds) del (reproj_ds) f.seek(0) return f.read()
def loadTerrain(directory_terrain, cfg): ''' Loads a terrain data file using GDAL INPUTS directory_terrain: Path to a folder containing one or more DTED files in .dt2 format OUTPUT Returns north east and elevation values as three 2D grids ''' if (cfg['terrain']['source'] == 'DTED'): # Open DTED files (merge if multiple) ds = gdal.BuildVRT('', glob.glob(directory_terrain + '/*.dt2')) # GDAL affine transform parameters, According to gdal documentation xoff/yoff are image left corner, a/e are pixel wight/height and b/d is rotation and is zero if image is north up. xoff, a, b, yoff, d, e = ds.GetGeoTransform() def pixel2coord(x, y, a, b, xoff, yoff, d, e): """Returns global coordinates from pixel x, y coords""" xp = a * x + b * y + xoff yp = d * x + e * y + yoff return (xp, yp) # Get corner coordinates longitude, latitude = pixel2coord(0, 0, a, b, xoff, yoff, d, e) # Transform from lat lon to UTM Coordinates def utm_getZone(longitude): return (int(1 + (longitude + 180.0) / 6.0)) def utm_isNorthern(latitude): if (latitude < 0.0): return 0 else: return 1 # Define target SRS utm_cs = osr.SpatialReference() utm_cs.SetWellKnownGeogCS('WGS84') utm_cs.SetUTM(utm_getZone(longitude), utm_isNorthern(latitude)) dst_wkt = utm_cs.ExportToWkt() error_threshold = 0.125 # error threshold --> use same value as in gdalwarp resampling = gdal.GRA_NearestNeighbour # Call AutoCreateWarpedVRT() to fetch default values for target raster dimensions and geotransform utm_ds = gdal.AutoCreateWarpedVRT( ds, None, # src_wkt : left to default value --> will use the one from source dst_wkt, resampling, error_threshold) # Recalculate affine transformation xoff, a, b, yoff, d, e = utm_ds.GetGeoTransform() # Get elevation data dd = utm_ds.ReadAsArray() # Calculate vertical datum adjustment from MSL to WGS84 msl2Wgs_adjustment = msl2Wgs(latitude, longitude) dd = dd + msl2Wgs_adjustment # Get Northing and Easting Grids rows, cols = dd.shape nn = np.zeros(dd.shape) ee = np.zeros(dd.shape) for row in range(0, rows): for col in range(0, cols): east, north = pixel2coord(col, row, a, b, xoff, yoff, d, e) nn[row, col] = north ee[row, col] = east elif (cfg['terrain']['source'] == 'PLY'): filename = glob.glob(directory_terrain + '/*.ply')[0] nn, ee, dd = loadply(filename) terrain = pd.DataFrame terrain.nn = nn terrain.ee = ee terrain.dd = dd return terrain
def reproject(self, proj, resampling=3, threshold=0.125): """ Reproject raster and save in a virtual raster INPUTS proj [int, str] proj can be a EPSG integer code or a Well Known string projection resampling [int] resampling method [0] Nearest Neighbour [1] Averange [2] Bilinear [3] Cubic (default) [4] Cubic Spline OUTPUTS newobj [GridObj] reprojected virtual GridObj """ if type(self.driver) is not _gdal.Dataset: # it is a valid gdal dataset? raise TypeError('You must connect with a raster file!') # Default resampling methods methods = [ _gdalconst.GRA_NearestNeighbour, _gdalconst.GRA_Average, _gdalconst.GRA_Bilinear, _gdalconst.GRA_Cubic, _gdalconst.GRA_CubicSpline ] resampling = int(resampling) assert 0 <= resampling <= 4 method = methods[resampling] # Get projection if type(proj) is int: proj = _crs_from_epsg(proj) elif type(proj) is not str: raise TypeError('Bad proj parameter type {}. proj must be an EPSG' 'integer code or a Well Known string'.format(str(type(proj)))) # Fetch default values for target raster dimensions and geotransform tmp_ds = _gdal.AutoCreateWarpedVRT(self.driver, None, # use self.projectionref proj, method, threshold) # Project raster dest = _gdal.GetDriverByName('MEM').CreateCopy('', tmp_ds) # Create new grid obj newobj = GridObj() # create new grid object newobj.driver = dest # set connection newobj.filename = '' newobj.divername = 'MEM' newobj.bands = dest.RasterCount newobj.geotransform = dest.GetGeoTransform() newobj.projectionref = dest.GetProjectionRef() newobj.xsize = dest.RasterXSize newobj.ysize = dest.RasterYSize # Close files del(dest) band_data = None return(newobj) # resample()