def main(): parser = get_parser() args = parser.parse_args() # comput number of physical and threaded cores n_cpu = psutil.cpu_count(logical=False) n_cpu_thread = psutil.cpu_count(logical=True) mode = args.mode pc_list = args.point_cloud_list if mode == 'gridding_only': tr = args.tr if args.tsrs is not None: tsrs = args.tsrs else: print("Projected Target CRS not provided, reading from the first point cloud") pc_ds = iolib.fn_getds(pc_list[0]) wgs_srs = osr.SpatialReference() wgs_srs.ImportFromEPSG(4326) clon,clat = geolib.get_center(pc_ds,t_srs=wgs_srs) epsg_code = f'EPSG:{geo.compute_epsg(clon,clat)}' print(f"Detected EPSG code from point cloud {epsg_code}") tsrs = epsg_code point2dem_opts = asp.get_point2dem_opts(tr=tr, tsrs=tsrs,threads=1) job_list = [point2dem_opts + [pc] for pc in pc_list] p2dem_log = p_map(asp.run_cmd,['point2dem'] * len(job_list), job_list, num_cpus = n_cpu) print(p2dem_log) if mode == 'classic_dem_align': ref_dem=args.refdem source_dem=args.source_dem max_displacement=args.max_displacement outprefix=args.outprefix align=args.align if args.trans_only == 0: trans_only=False else: trans_only=True asp.dem_align(ref_dem, source_dem, max_displacement, outprefix, align, trans_only,threads=n_cpu) if mode == 'multi_align': """ Align multiple DEMs to a single source DEM """ ref_dem=args.refdem source_dem_list=args.source_dem_list max_displacement=args.max_displacement outprefix_list=['{}_aligned_to{}'.format(os.path.splitext(source_dem)[0],os.path.splitext(os.path.basename(ref_dem))[0]) for source_dem in source_dem_list] align=args.align if args.trans_only == 0: trans_only=False else: trans_only=True n_source=len(source_dem_list) ref_dem_list=[ref_dem] * n_source max_disp_list=[max_displacement] * n_source align_list=[align] * n_source trans_list=[trans_only] * n_source p_umap(asp.dem_align,ref_dem_list,source_dem_list,max_disp_list,outprefix_list,align_list,trans_list,[1]*n_source,num_cpus = n_cpu_thread) if mode == 'align_cameras': transform_txt = args.transform input_camera_list = args.cam_list n_cam=len(input_camera_list) if (args.rpc == 1) & (args.dem != 'None'): print("will also write rpc files") dem=args.dem img_list=arg.img_list rpc=True else: dem=None img_list=[None] * n_cam rpc=False transform_list=[transform_txt] * n_cam outfolder = args.outfol if not os.path.exists(outfolder): os.makedirs(outfolder) outfolder=[outfolder] * n_cam write=[True] * n_cam rpc=[rpc] * n_cam dem=[dem] * n_cam p_umap(asp.align_cameras,input_camera_list,transform_list,outfolder,write,rpc,dem,img_list,num_cpus = n_cpu_thread)
xMin = np.min(lons) yMax = np.max(lats) yMin = np.min(lats) #array = no2 filename = 'C:\\temp\data\output.tif' #arrayToRaster(array,'C:\\temp\data\output.tif',EPSGCode,xMin,xMax,yMin,yMax,numBands): xPixels = lons.shape[1] # number of pixels in x yPixels = lons.shape[0] # number of pixels in y pixelXSize = (xMax - xMin) / xPixels # size of the pixel in X direction pixelYSize = -(yMax - yMin) / yPixels # size of the pixel in Y direction dataset = gdal.GetDriverByName('GTiff').Create('C:\\temp\data\output.tif', xPixels, yPixels, numBands, gdal.GDT_Float32) dataset.SetGeoTransform((xMin, pixelXSize, 0, yMax, 0, pixelYSize)) datasetSRS = osr.SpatialReference() datasetSRS.ImportFromEPSG(EPSGCode) dataset.SetProjection(datasetSRS.ExportToWkt()) dataset.GetRasterBand(1).WriteArray(no2) dataset = None # Write to disk. # # driver = gdal.GetDriverByName('GTiff') # dataset = driver.Create(fileName,xPixels,yPixels,numBands,gdal.GDT_Byte, options = [ 'PHOTOMETRIC=RGB' ]) # dataset.SetGeoTransform((xMin,pixelXSize,0,yMax,0,pixelYSize)) # # datasetSRS = osr.SpatialReference() # datasetSRS.ImportFromEPSG(EPSGCode) # dataset.SetProjection(datasetSRS.ExportToWkt()) # # for i in range(0,numBands): # dataset.GetRasterBand(i+1).WriteArray(array[:,:,i])
def writeShp(outFileName): # 为了支持中文路径,请添加下面这句代码 # gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO") # # 为了使属性表字段支持中文,请添加下面这句 # gdal.SetConfigOption("SHAPE_ENCODING", "") gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES") gdal.SetConfigOption("SHAPE_ENCODING", "GBK") # 1.创建输出文件 srs = osr.SpatialReference() # 创建空间参考 srs.ImportFromEPSG(4490) # 定义地理坐标系CGCS2000 outdriver = ogr.GetDriverByName('ESRI Shapefile') if os.path.exists(outFileName): outdriver.DeleteDataSource(outFileName) outds = outdriver.CreateDataSource(outFileName) oLayer = outds.CreateLayer(outFileName, srs, geom_type=ogr.wkbPoint) if oLayer == None: print("图层创建失败!\n") return # 2.下面创建属性表 # 2.1 创建一个叫FieldID的整型属性 fruitId = ogr.FieldDefn("fruitId", ogr.OFTInteger) oLayer.CreateField(fruitId, 1) # 2.2 创建一个叫坐标X的浮点属性 x_coord = ogr.FieldDefn("x_coord", ogr.OFTString) oLayer.CreateField(x_coord, 1) # 2.3 创建一个叫坐标y的浮点属性 y_coord = ogr.FieldDefn("y_coord", ogr.OFTString) oLayer.CreateField(y_coord, 1) # 2.4 创建一个叫fruitCategoryId的整型属性 CategoryId = ogr.FieldDefn("CategoryId", ogr.OFTInteger) oLayer.CreateField(CategoryId, 1) # 2.5 创建一个叫pointName的字符型属性 pointName = ogr.FieldDefn("pointName", ogr.OFTString) #oFieldName.SetWidth(100) oLayer.CreateField(pointName, 1) # 2.6 创建一个叫pointLevel的字符型属性 pointLevel = ogr.FieldDefn("pointLevel", ogr.OFTString) oLayer.CreateField(pointLevel, 1) # 2.7 创建一个叫geoDatum的字符型属性 highDatum = ogr.FieldDefn("highDatum", ogr.OFTString) oLayer.CreateField(highDatum, 1) # 2.8 创建一个叫producDate的字符型属性 producDate = ogr.FieldDefn("producDate", ogr.OFTString) oLayer.CreateField(producDate, 1) #3.写入属性 outfielddefn = oLayer.GetLayerDefn() if len(locationList) == len(attributeList): row = 0 while row < len(locationList): # 按行处理 dic = locationList[row] #str = attributeList[row].get('geoDatum') str = attributeList[row]['highDatum'] #print(row,str) if str == '1985国家高程基准': attribute = attributeList[row] for key in attribute: if dic.get(key): pass else: dic[key] = attribute[key] # 每一行的每一个元素 #print(dic) # 创建点属性 outfeat = ogr.Feature(outfielddefn) #添加字段属性 col = 0 for jj in dic.values(): outfeat.SetField(col, jj) col += 1 # 创建要素,写入多边形 point = ogr.Geometry(ogr.wkbPoint) # 构建几何类型:点 point_x = float(dic['x']) point_y = float(dic['y']) point.AddPoint(point_x, point_y) # 创建点 outfeat.SetGeometry(point) # 写入图层 oLayer.CreateFeature(outfeat) #oFeaturePint = None row += 1
def ogr_pcidsk_1(): ogr_drv = ogr.GetDriverByName('PCIDSK') if ogr_drv is None: return 'skip' ds = ogr_drv.CreateDataSource('tmp/ogr_pcidsk_1.pix') lyr = ds.CreateLayer('nothing', geom_type=ogr.wkbNone) feat = ogr.Feature(lyr.GetLayerDefn()) lyr.CreateFeature(feat) lyr.ResetReading() feat = lyr.GetNextFeature() if feat is None: gdaltest.post_reason('failure') return 'fail' lyr = ds.CreateLayer('fields', geom_type=ogr.wkbNone) lyr.CreateField(ogr.FieldDefn('strfield', ogr.OFTString)) lyr.CreateField(ogr.FieldDefn('intfield', ogr.OFTInteger)) lyr.CreateField(ogr.FieldDefn('realfield', ogr.OFTReal)) feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetField(0, 'foo2') feat.SetField(1, 1) feat.SetField(2, 3.45) lyr.CreateFeature(feat) feat.SetField(0, 'foo') lyr.SetFeature(feat) feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetField(0, 'bar') lyr.CreateFeature(feat) if lyr.GetFeatureCount() != 2: gdaltest.post_reason('failure') return 'fail' lyr.DeleteFeature(1) if lyr.GetFeatureCount() != 1: gdaltest.post_reason('failure') return 'fail' lyr.ResetReading() feat = lyr.GetNextFeature() if feat is None: gdaltest.post_reason('failure') return 'fail' if feat.GetField(0) != 'foo': gdaltest.post_reason('failure') return 'fail' if feat.GetField(1) != 1: gdaltest.post_reason('failure') return 'fail' if feat.GetField(2) != 3.45: gdaltest.post_reason('failure') return 'fail' for (wkt, layername, epsgcode) in wkts: geom = ogr.CreateGeometryFromWkt(wkt) if epsgcode != 0: srs = osr.SpatialReference() srs.ImportFromEPSG(epsgcode) else: srs = None lyr = ds.CreateLayer(layername, geom_type=geom.GetGeometryType(), srs=srs) feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetGeometry(geom) lyr.CreateFeature(feat) lyr.ResetReading() feat = lyr.GetNextFeature() if feat is None: gdaltest.post_reason('failure') print(layername) return 'fail' if feat.GetGeometryRef().ExportToWkt() != wkt: gdaltest.post_reason('failure') feat.DumpReadable() print(layername) return 'fail' ds = None return 'success'
def is_geographic(self): """ Returns whether the coords are geographic based on proj4 """ sr = osr.SpatialReference() sr.ImportFromProj4(self.srs) return bool(sr.IsGeographic())
def main(ingpxfile, outpntsshapefile, secdiff): ## infile = "GPSTrack_D5T4.gpx" ## outfile = "GPSTrack_D5T4_utm6_with_segments.shp" ## Read in time data for each ASD file. ## they are stored in a pre-made list with the ASD file name and the date/Time in each row csvtablefile = "/Carnegie/DGE/caodata/Scratch/dknapp/ASD/Spectroscopy/list_spec_time.txt" specrows = [] with open(csvtablefile, 'r') as csvfile: csvreader = csv.reader(csvfile) for row in csvreader: specrows.append(row) transroot = os.path.splitext(ingpxfile)[0][-4:] + "*.asd.ref" ## Create a list with the date/times as datetime objects. spectimedates = [] for row in specrows: ## uggh. The format for timezone offset (%z) does not include a colon (:), ## so we have to skip that colon character. temp = row[1][0:22] + row[1][23:] trydate = datetime.datetime.strptime(temp, "%Y-%m-%dT%H:%M:%S%z") spectimedates.append(trydate) ## CReate output spatial reference for Moorea (UTM Zone 6 South) spatialReference = osr.SpatialReference() spatialReference.ImportFromEPSG(32706) ## Create output data file drv = ogr.GetDriverByName("ESRI Shapefile") outDS = drv.CreateDataSource(outpntsshapefile) ## outlayer = outDS.CreateLayer('moorea', spatialReference, ogr.wkbLineString) outlayer = outDS.CreateLayer('moorea', spatialReference, ogr.wkbPoint) outlayerDefn = outlayer.GetLayerDefn() tnameDefn = ogr.FieldDefn('specname', ogr.OFTString) timepntDefn = ogr.FieldDefn('timepnt', ogr.OFTString) outlayer.CreateField(tnameDefn) outlayer.CreateField(timepntDefn) ## Get input data layer (track_points) inDS = ogr.Open(ingpxfile) lyr = inDS.GetLayerByName('track_points') lyrdefn = lyr.GetLayerDefn() numpnts = lyr.GetFeatureCount() fldcnt = lyrdefn.GetFieldCount() projutm6s = pyproj.Proj("+init=EPSG:32706") pntutm = [] times = [] azimuths = [] lyr.ResetReading() ## create utc and french polynesia timezone objects utc = datetime.timezone.utc fptz = datetime.timezone(datetime.timedelta(hours=-10)) for k in range(0, numpnts): feat = lyr.GetFeature(k) mytime = feat.GetFieldAsDateTime('time') print(mytime) mydatetime = datetime.datetime(mytime[0], mytime[1], mytime[2], mytime[3], \ mytime[4], int(mytime[5]), tzinfo=utc) geom = feat.GetGeometryRef() lon = geom.GetX() lat = geom.GetY() temputm = projutm6s(lon, lat) pntutm.append(temputm) times.append(mydatetime) for j in np.arange(0, (len(pntutm) - 1)): pnt1 = pntutm[j] pnt2 = pntutm[j + 1] diffx = pnt2[0] - pnt1[0] diffy = pnt2[1] - pnt1[1] initial_azimuth = math.degrees(math.atan2(diffx, diffy)) azimuth = (initial_azimuth + 360) % 360 azimuths.append(azimuth) time1 = times[j] time2 = times[j + 1] segtimediff = (time2 - time1).total_seconds() segdist = math.sqrt(math.pow(diffx, 2) + math.pow(diffy, 2)) ## Find the spectra that are between these 2 points myregex = fnmatch.translate(transroot) asdobj = re.compile(myregex) for i, asdrow in enumerate(specrows): gotit = asdobj.match(asdrow[0]) if gotit is not None: spectime = spectimedates[i] + datetime.timedelta( seconds=secdiff) # is it between these 2 segment points? if (spectime > time1) and (spectime < time2): propo = ((spectime - time1).total_seconds()) / float(segtimediff) azrad = azimuth * (math.pi / 180.0) xlen = math.sin(azrad) * (propo * segdist) ylen = math.cos(azrad) * (propo * segdist) xnewpnt = pnt1[0] + xlen ynewpnt = pnt1[1] + ylen feature = ogr.Feature(outlayerDefn) pnt = ogr.Geometry(ogr.wkbPoint) pnt.AddPoint(xnewpnt, ynewpnt) feature.SetGeometry(pnt) feature.SetFID(i) feature.SetField('specname', asdrow[0]) timestr = spectime.astimezone(utc).strftime( "%Y-%m-%dT%H:%M:%SZ") feature.SetField('timepnt', timestr) outlayer.CreateFeature(feature) inDS, outDS = None, None
from glob import glob import gdal, osr, ogr data_source = '~/Desktop/FF_STATS/maps/LCType.tif' point = ogr.Geometry(ogr.wkbPoint) # Specify that the point uses the WGS84 reference system sr = osr.SpatialReference() sr.ImportFromEPSG(4326) point.AssignSpatialReference(sr) # Point's co-ordinates (in WGS84 it's latitude and longitude) point.AddPoint(36.16469217, -86.77208918) def extract_point_from_raster(point, data_source, band_number=1): """Return floating-point value that corresponds to given point.""" # Convert point co-ordinates so that they are in same projection as raster point_sr = point.GetSpatialReference() raster_sr = osr.SpatialReference() raster_sr.ImportFromWkt(data_source.GetProjection()) transform = osr.CoordinateTransformation(point_sr, raster_sr) point.Transform(transform) # Convert geographic co-ordinates to pixel co-ordinates x, y = point.GetX(-86.77208918), point.GetY(36.16469217) forward_transform = Affine.from_gdal(*data_source.GetGeoTransform()) reverse_transform = ~forward_transform
def get_skelton(tifname, dso=None, name_use=None, fn_censor=None): if name_use is None: name_use = tifname name_use = os.path.basename(name_use) ds = gdal.Open(tifname) gt = np.array(ds.GetGeoTransform()) nc = ds.RasterXSize nr = ds.RasterYSize # get projection srs = osr.SpatialReference() srs.ImportFromWkt(ds.GetProjection()) # if its sinusoidal, i know what to do if fn_censor is None: srs0 = osr.SpatialReference() srs0.ImportFromProj4('+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs') #print(srs) #print(srs0) # this IsSame doesnt work... if srs.IsSame(srs0): # given y coord, i can calculate length of the parallel. # if x coord is beyond what's expected, shift to the x bound fn_censor = censor_sinu else: fn_censor = lambda x: x # get corners and points along sides # points on one side num = 50 xp = np.rint(np.linspace(0, nc, num+1)) yp = np.rint(np.linspace(0, nr, num+1)) # coords along four sides xy = np.zeros(((num)*4+1,2)) xy[(0*num):(1*num),0] = 0 xy[(0*num):(1*num),1] = yp[:-1] xy[(1*num):(2*num),0] = xp[:-1] xy[(1*num):(2*num),1] = yp[-1] xy[(2*num):(3*num),0] = xp[-1] xy[(2*num):(3*num),1] = yp[:0:-1] xy[(3*num):(4*num),0] = xp[:0:-1] xy[(3*num):(4*num),1] = 0 xy[(4*num),:]=0 # inver order so that points go clockwise, somwhow it works better in shapely # with original ordering, it failed to fix the shape in siberia ('h22v01'), # for example. top right tile which tourches boundary seemed to fail # better way is to not rely on censor method, which shift out of boundary points # horizontally to boundary but also check if vertical correction is needed # (move to intersection of side of tile and the boundary), but i just let it # go for now, it is sort of working # xy = xy[::-1,:] # coords in dataset's coordinate xy = np.apply_along_axis(lambda p: (gt[0] + (gt[1:3]*p).sum(), gt[3] + (gt[4:6]*p).sum()), 1, xy) # censor points outside of defined area xy = fn_censor(xy) # remove repeated points (after censoring) ok = [0] for i in range(1, (xy.shape[0])): if not np.array_equal(xy[i-1,:], xy[i,:]): ok.append(i) xy = xy[ok,:] # make it into polygon poly = Polygon(xy) if not poly.is_valid: poly = poly.buffer(0) if not poly.is_valid: import pdb; pdb.set_trace() assert poly.area > 0 # ogr memory dataset if dso is None: # create dataset drv = ogr.GetDriverByName('Memory') dso = drv.CreateDataSource('poly') lyr = dso.CreateLayer('',srs,ogr.wkbPolygon) lyr.CreateField(ogr.FieldDefn('id',ogr.OFTInteger)) fdefn = ogr.FieldDefn('name',ogr.OFTString) fdefn.SetWidth(255) lyr.CreateField(fdefn) defn = lyr.GetLayerDefn() idn = 1 else: # last record lyr = dso.GetLayer() defn = lyr.GetLayerDefn() idn = lyr.GetFeatureCount() idn = max(idn, lyr.GetFeature(idn-1).GetField('id')) + 1 # add the polygon to the dataset feat = ogr.Feature(defn) feat.SetField('id', idn) feat.SetField('name', name_use) geom = ogr.CreateGeometryFromWkb(poly.wkb) feat.SetGeometry(geom) lyr.CreateFeature(feat) feat = geom = None lyr = None return dso
def work_polygonize(self, tifnames, dstdir, bname, dryrun=False): # create vrt first, and then generate tiled warped files if not os.path.exists(dstdir): os.makedirs(dstdir) # create vrtual dataset vrtname = os.path.join(dstdir, 'src.vrt') #cmd = 'gdalbuildvrt %s %s' % ( vrtname, ' '.join(tifnames)) # anaconda on win had trouble with long command line, ,so rewrote with -input_file_ist with open('tifnames.txt','w') as f: f.write('\n'.join(tifnames) + '\n') cmd = 'gdalbuildvrt %s -input_file_list %s' % ( vrtname, 'tifnames.txt') status = os.system(cmd) if status != 0: raise RuntimeError('exit status %s, cmd = %s' % (status, cmd)) # open the unified band ds = gdal.Open(vrtname) srs0 = ds.GetProjection() #print(srs0) #print(ds) b = ds.GetRasterBand(1) # create mask band # valid data are 1-366. 0 is unbunred, -1 fill, -2 water # mask file should has 1 for valid, 0 for invalid drv = gdal.GetDriverByName('MEM') dsm = drv.CreateCopy('mem0', ds) m = dsm.GetRasterBand(1) arr = m.ReadAsArray() arr[arr<0] = 0 m.WriteArray(arr) oname0 = os.path.join(dstdir, '.'.join([bname, 'sinu','shp'])) oname = os.path.join(dstdir, '.'.join([bname, 'shp'])) #print(oname0) #print(oname) # output shapefile #drv = ogr.GetDriverByName('Memory') drv = ogr.GetDriverByName('ESRI Shapefile') if os.path.exists(oname0): drv.DeleteDataSource(oname0) dst0 = drv.CreateDataSource(oname0) srs = osr.SpatialReference() srs.ImportFromWkt(srs0) #print(dst0) #print(ogr) lyr0 = dst0.CreateLayer('lyr0', srs, ogr.wkbPolygon) #print(lyr0) fd = ogr.FieldDefn('BurnYear', ogr.OFTInteger) lyr0.CreateField(fd) fd = ogr.FieldDefn('BurnDate', ogr.OFTInteger) lyr0.CreateField(fd) fd = ogr.FieldDefn('area_sqkm', ogr.OFTReal) lyr0.CreateField(fd) fld = 1 #second field gdal.Polygonize(b, m, lyr0, fld, [], callback=None) del b, m # band del ds, dsm # raster lyr0.SetNextByIndex(0) for i,feat in enumerate(lyr0): feat.SetField('BurnYear', self.year) geom = feat.GetGeometryRef() feat.SetField('area_sqkm', geom.GetArea() / 1000000) lyr0.SetFeature(feat) # project target_projection = '+proj=longlat +datum=WGS84 +no_defs' drv = ogr.GetDriverByName('ESRI Shapefile') if os.path.exists(oname): drv.DeleteDataSource(oname) srs1 = osr.SpatialReference() srs1.ImportFromProj4(target_projection) dst = transform_coordinates(dst0, srs1, drv, oname=oname) del lyr0 del dst0 return dst
def ogr_gml_21(format='GML3'): if not gdaltest.have_gml_reader: return 'skip' # Create GML3 file sr = osr.SpatialReference() sr.ImportFromEPSG(4326) for filename in ['tmp/gml_21.gml', 'tmp/gml_21.xsd', 'tmp/gml_21.gfs']: try: os.remove(filename) except: pass ds = ogr.GetDriverByName('GML').CreateDataSource( 'tmp/gml_21.gml', options=['FORMAT=' + format]) lyr = ds.CreateLayer('firstlayer', srs=sr) lyr.CreateField(ogr.FieldDefn('string_field', ogr.OFTString)) feat = ogr.Feature(lyr.GetLayerDefn()) geom = ogr.CreateGeometryFromWkt('POINT (2 49)') feat.SetGeometry(geom) lyr.CreateFeature(feat) feat = ogr.Feature(lyr.GetLayerDefn()) feat.SetField(0, 'foo') geom = ogr.CreateGeometryFromWkt('POINT (3 48)') feat.SetGeometry(geom) lyr.CreateFeature(feat) ds = None # Reopen the file ds = ogr.Open('tmp/gml_21.gml') lyr = ds.GetLayer(0) feat = lyr.GetNextFeature() if feat.GetGeometryRef().ExportToWkt() != 'POINT (2 49)': gdaltest.post_reason('did not get expected geometry') return 'fail' ds = None # Test that .gml and .xsd are identical to what is expected f1 = open('tmp/gml_21.gml', 'rt') f2 = open('data/expected_gml_21.gml', 'rt') line1 = f1.readline() line2 = f2.readline() while line1 != '': line1 = line1.strip() line2 = line2.strip() if line1 != line2: gdaltest.post_reason('.gml file not identical to expected') print(open('tmp/gml_21.gml', 'rt').read()) return 'fail' line1 = f1.readline() line2 = f2.readline() f1.close() f2.close() f1 = open('tmp/gml_21.xsd', 'rt') if format == 'GML3': f2 = open('data/expected_gml_21.xsd', 'rt') else: f2 = open('data/expected_gml_21_deegree3.xsd', 'rt') line1 = f1.readline() line2 = f2.readline() while line1 != '': line1 = line1.strip() line2 = line2.strip() if line1 != line2: gdaltest.post_reason('.xsd file not identical to expected') print(open('tmp/gml_21.xsd', 'rt').read()) return 'fail' line1 = f1.readline() line2 = f2.readline() f1.close() f2.close() return 'success'
def reproject_dataset_example(dataset, dataset_example, method=1): """ A sample function to reproject and resample a GDAL dataset from within Python. The user can define the wanted projection and shape by defining an example dataset. Keywords arguments: dataset -- 'C:/file/to/path/file.tif' or a gdal file (gdal.Open(filename)) string that defines the input tiff file or gdal file dataset_example -- 'C:/file/to/path/file.tif' or a gdal file (gdal.Open(filename)) string that defines the input tiff file or gdal file method -- 1,2,3,4 default = 1 1 = Nearest Neighbour, 2 = Bilinear, 3 = lanzcos, 4 = average """ # open dataset that must be transformed try: if (os.path.splitext(dataset)[-1] == '.tif' or os.path.splitext(dataset)[-1] == '.TIF'): g = gdal.Open(dataset) else: g = dataset except: g = dataset epsg_from = Get_epsg(g) #exceptions if epsg_from == 9001: epsg_from = 5070 # open dataset that is used for transforming the dataset try: if (os.path.splitext(dataset_example)[-1] == '.tif' or os.path.splitext(dataset_example)[-1] == '.TIF'): gland = gdal.Open(dataset_example) epsg_to = Get_epsg(gland) else: gland = dataset_example epsg_to = Get_epsg(gland) except: gland = dataset_example epsg_to = Get_epsg(gland) # Set the EPSG codes osng = osr.SpatialReference() osng.ImportFromEPSG(epsg_to) wgs84 = osr.SpatialReference() wgs84.ImportFromEPSG(epsg_from) # Get shape and geo transform from example geo_land = gland.GetGeoTransform() col = gland.RasterXSize rows = gland.RasterYSize # Create new raster mem_drv = gdal.GetDriverByName('MEM') dest1 = mem_drv.Create('', col, rows, 1, gdal.GDT_Float32) dest1.SetGeoTransform(geo_land) dest1.SetProjection(osng.ExportToWkt()) # Perform the projection/resampling if method == 1: gdal.ReprojectImage(g, dest1, wgs84.ExportToWkt(), osng.ExportToWkt(), gdal.GRA_NearestNeighbour) if method == 2: gdal.ReprojectImage(g, dest1, wgs84.ExportToWkt(), osng.ExportToWkt(), gdal.GRA_Bilinear) if method == 3: gdal.ReprojectImage(g, dest1, wgs84.ExportToWkt(), osng.ExportToWkt(), gdal.GRA_Lanczos) if method == 4: gdal.ReprojectImage(g, dest1, wgs84.ExportToWkt(), osng.ExportToWkt(), gdal.GRA_Average) return (dest1)
def main(indovedir, outparamfile): inhdfdir = "/home/dknapp4/MOD08_data/" filelist = os.listdir(inhdfdir) doveroot = '2*3B_AnalyticMS.tif' doveregex = fnmatch.translate(doveroot) dovereobj = re.compile(doveregex) dovelist = [] filelist2 = os.listdir(indovedir) for dovefile in filelist2: gotit = dovereobj.match(dovefile) if gotit is not None: dovelist.append(dovefile) print(("Finished making list of Planet Dove images: %d" % len(dovelist))) paramlist = [] f = open(outparamfile, "w") for indove in dovelist: doveds = gdal.Open(indovedir + "/" + indove, gdal.GA_ReadOnly) basedove = os.path.basename(indove) year = int(basedove[0:4]) month = int(basedove[4:6]) day = int(basedove[6:8]) imgdate = datetime.date(year, month, day).timetuple().tm_yday hdfroot = 'MOD08_D3.A' + (("%04d%03d") % (year, imgdate)) regex = fnmatch.translate(hdfroot + '*.hdf') reobj = re.compile(regex) hdffile = None for filename in filelist: gotit = reobj.match(filename) if gotit is not None: hdffile = inhdfdir + "/" + filename break if hdffile is None: print(("HDF file matching " + hdfroot + " not found for Dove %s." % (basedove))) continue print(("HDF file found: %s " % (hdffile))) modisds = gdal.Open(hdffile, gdal.GA_ReadOnly) moddatalist = modisds.GetSubDatasets() aotcard = "Aerosol_Optical_Depth_Average_Ocean_Mean" wvcard = "Atmospheric_Water_Vapor_Mean" ozcard = "Total_Ozone_Mean" aotregex = fnmatch.translate(aotcard) wvregex = fnmatch.translate(wvcard) ozregex = fnmatch.translate(ozcard) aotobj = re.compile(aotregex) wvobj = re.compile(wvregex) ozobj = re.compile(ozregex) for subdataset in moddatalist: aotgotit = aotobj.match(subdataset[0].split(':')[4]) if aotgotit is not None: aotsub = subdataset[0] continue for subdataset in moddatalist: wvgotit = wvobj.match(subdataset[0].split(':')[4]) if wvgotit is not None: wvsub = subdataset[0] continue for subdataset in moddatalist: ozgotit = ozobj.match(subdataset[0].split(':')[4]) if ozgotit is not None: ozsub = subdataset[0] break aotimg = gdal.Open(aotsub, gdal.GA_ReadOnly).ReadAsArray() wvimg = gdal.Open(wvsub, gdal.GA_ReadOnly).ReadAsArray() ozimg = gdal.Open(ozsub, gdal.GA_ReadOnly).ReadAsArray() ## get center lat lon of file dovegt = doveds.GetGeoTransform() ulutmx = dovegt[0] ulutmy = dovegt[3] centerutmx = ulutmx + ((doveds.RasterXSize / 2.) * dovegt[1]) centerutmy = ulutmy + ((doveds.RasterYSize / 2.) * dovegt[5]) projinfo = osr.SpatialReference() projinfo.ImportFromWkt(doveds.GetProjectionRef()) projutm = pyproj.Proj(projinfo.ExportToProj4()) centerlonlat = projutm(centerutmx, centerutmy, inverse=True) ## get pixel and line col = np.floor(centerlonlat[0] + 180.0).astype('int') row = np.floor(90.0 - centerlonlat[1]).astype('int') print(("Center Lon/Lat is: %10.5f %10.5f" % (centerlonlat[0], centerlonlat[1]))) print(("Center Row/Col is: %d %d" % (row, col))) if (aotimg[1, row, col] == -9999.0): print("AOT: missing, need to use interpolated value") ## make mask of good data good = np.squeeze(np.not_equal(aotimg[1, :, :], -9999.0)) roworig, colorig = np.indices(good.shape) rowgood = roworig[good] colgood = colorig[good] gridit = griddata(np.column_stack((colgood, rowgood)), aotimg[1, rowgood, colgood].flatten(), (col, row), method='linear').item() aotval = gridit / 1000.0 else: aotval = aotimg[1, row, col] / 1000.0 if (wvimg[row, col] == -9999.0): print("WV: missing, need to use interpolated value") ## make mask of good data good = np.squeeze(np.not_equal(wvimg, -9999.0)) roworig, colorig = np.indices(good.shape) rowgood = roworig[good] colgood = colorig[good] gridit = griddata(np.column_stack((colgood, rowgood)), wvimg[good].flatten(), (col, row), method='linear').item() wvval = gridit / 1000.0 else: wvval = wvimg[row, col] / 1000.0 if (ozimg[row, col] == -9999.0): print("Ozone: missing, need to use interpolated value") ## make mask of good data good = np.squeeze(np.not_equal(ozimg, -9999.0)) roworig, colorig = np.indices(good.shape) rowgood = roworig[good] colgood = colorig[good] gridit = griddata(np.column_stack((colgood, rowgood)), ozimg[good].flatten(), (col, row), method='linear').item() ozval = gridit / 10000.0 else: ozval = ozimg[row, col] / 10000.0 print("Extracted Values:") print(("AOT: %8.4f WV: %8.4f Ozone: %8.4f" % (aotval, wvval, ozval))) ## bigstring = "%s, %8.4f, %8.4f, %8.4f" % (basedove, aotval, wvval, ozval) aotstring = "%8.4f" % (aotval) wvstring = "%8.4f" % (wvval) ozstring = "%8.4f" % (ozval) ## paramlist.append((basedove, aotstring, wvstring, ozstring)) f.write("%s, %8.4f, %8.4f, %8.4f\n" % (basedove, aotval, wvval, ozval)) modisds, doveds = None, None f.close()
def _parse_geometries( census_boundaries_dir_path: typing.AnyStr, project_epsg3347_to_epsg4326: bool = True, ): census_boundaries_file_path = covid19geo.utils.get_unique_file_by_extension( directory_path=census_boundaries_dir_path, extension=".shp", ) logger.debug( f"parsing boundaries shapefile: {census_boundaries_file_path}") import ogr # we import gdal components just before use in case someone can't install ogr_driver = ogr.GetDriverByName("ESRI Shapefile") boundaries_fd = ogr_driver.Open(census_boundaries_file_path, 0) boundaries_layer = boundaries_fd.GetLayer() boundaries_count = boundaries_layer.GetFeatureCount() logger.debug(f"will load {boundaries_count} features") boundaries_layer_defs = boundaries_layer.GetLayerDefn() boundaries_attrib_count = boundaries_layer_defs.GetFieldCount() attribs_full_str = f"each feature has {boundaries_attrib_count} attributes:" found_dauid = False for i in range(boundaries_layer_defs.GetFieldCount()): field_name = boundaries_layer_defs.GetFieldDefn(i).GetName() field_type_code = boundaries_layer_defs.GetFieldDefn(i).GetType() field_type = boundaries_layer_defs.GetFieldDefn( i).GetFieldTypeName(field_type_code) field_width = boundaries_layer_defs.GetFieldDefn(i).GetWidth() field_precision = boundaries_layer_defs.GetFieldDefn( i).GetPrecision() field_str = \ f"{field_name}: {field_type} (width={field_width}, precision={field_precision})" attribs_full_str += f"\n\t{field_str}" if field_name == "DAUID": assert field_width == 8 and field_type == "String", \ "unexpected DAUID field width/type" found_dauid = True logger.debug(attribs_full_str) assert found_dauid, "features missing expected 'DAUID' field" # will load all dissemination areas into memory while keeping track of 3-level hierarchy dauid_map, cduid_map, ptuid_map = {}, {}, {} transform = None if project_epsg3347_to_epsg4326: import osr # we import gdal components just before use in case someone can't install source_ref, target_ref = osr.SpatialReference( ), osr.SpatialReference() source_ref.ImportFromEPSG(3347) assert source_ref.IsSame(boundaries_layer.GetSpatialRef()) target_ref.ImportFromEPSG(4326) transform = osr.CoordinateTransformation(source_ref, target_ref) for feature in tqdm.tqdm(boundaries_layer, desc="parsing features"): dauid = feature.GetField("DAUID") cduid, ptuid = dauid[0:4], dauid[0:2] geom = feature.GetGeometryRef() if transform is not None: geom.Transform(transform) geometry_ref = dict( ptuid=ptuid, cduid=cduid, dauid=dauid, wkb=geom.ExportToWkb(), ) assert dauid not in dauid_map, "unexpected duplicate DA geometry" dauid_map[dauid] = geometry_ref if cduid not in cduid_map: cduid_map[cduid] = [] cduid_map[cduid].append(geometry_ref) if ptuid not in ptuid_map: ptuid_map[ptuid] = [] ptuid_map[ptuid].append(geometry_ref) return dauid_map, cduid_map, ptuid_map
# print trX, trY # print brX, brY # build list of ground control points gcp_list = [ gdal.GCP(tlX, tlY, 0, 0, 0), gdal.GCP(trX, trY, 0, maxWidth, 0), gdal.GCP(brX, brY, 0, maxWidth, maxHeight), gdal.GCP(blX, blY, 0, 0, maxHeight), ] # open file with GDAL and write to them ds = gdal.Open('./cvCropped/noFrame/' + file) #, gdal.GA_Update) # 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,
# import modules import ogr, osr, os, sys # set the working directory os.chdir('f:/data/classes/python/data') # get the shapefile driver driver = ogr.GetDriverByName('ESRI Shapefile') # create the input SpatialReference inSpatialRef = osr.SpatialReference() inSpatialRef.ImportFromEPSG(4269) # create the output SpatialReference outSpatialRef = osr.SpatialReference() outSpatialRef.ImportFromEPSG(26912) # create the CoordinateTransformation coordTrans = osr.CoordinateTransformation(inSpatialRef, outSpatialRef) # open the input data source and get the layer inDS = driver.Open('hw2a.shp', 0) if inDS is None: print 'Could not open file' sys.exit(1) inLayer = inDS.GetLayer() # create a new data source and layer fn = 'hw2b.shp' if os.path.exists(fn): driver.DeleteDataSource(fn)
def work_resample_pieces(self, tifnames, dstdir, bname, hdfnames, dryrun=False): # create vrt first, and then generate tiled warped files if not os.path.exists(dstdir): os.makedirs(dstdir) target_projection = '+proj=longlat +datum=WGS84 +no_defs' # create sketlton # based on tif files, create polygon(s) representing region where data is avarilble ds_skelton = None for tifname,hdfname in zip(tifnames,hdfnames): ds_skelton = get_skelton(tifname, ds_skelton,name_use=hdfname, fn_censor=censor_sinu) #save_as_shp(ds_skelton, 'skely0.shp') # project skelton srs1 = osr.SpatialReference() srs1.ImportFromProj4(target_projection) ds_skelton = transform_coordinates(ds_skelton, srs1) #save_as_shp(ds_skelton, 'skely1.shp') if 'bdt' in self.shortnames: dso = self.work_polygonize(tifnames, dstdir, bname, dryrun) return [dso], ds_skelton intersector = Intersecter(ds_skelton) # create vrtual dataset vrtname = os.path.join(dstdir, 'src.vrt') #cmd = 'gdalbuildvrt %s %s' % ( vrtname, ' '.join(tifnames)) # anaconda on win had trouble with long command line, ,so rewrote with -input_file_ist with open('tifnames.txt','w') as f: f.write('\n'.join(tifnames) + '\n') cmd = 'gdalbuildvrt %s -input_file_list %s' % ( vrtname, 'tifnames.txt') status = os.system(cmd) if status != 0: raise RuntimeError('exit status %s, cmd = %s' % (status, cmd)) res = '-tr 0.00166666666666666666666666666667 0.00166666666666666666666666666667' # 6 sec prj = '-t_srs "%s"' % target_projection tiffopt = ' '.join(['-co %s' % _ for _ in tiffopts]) onames = [] for i in range(36): for j in range(18): c = (-180 + 10*i, 90 - 10*(j+1), -180+10*(i+1), 90-10*j) te = '-te %s' % ' '.join(str(_) for _ in c) oname = os.path.join(dstdir, '.'.join([bname, 'r%02dc%02d' % (i, j), 'tif'])) # if tile does not overlap with any skelton polygons, dont resample poly = Polygon([[c[0],c[1]],[c[0],c[3]],[c[2],c[3]],[c[2],c[1]],[c[0],c[1]]]) intsct = intersector(poly) if intsct.IsEmpty(): continue cmd = ( 'gdalwarp %(prj)s %(res)s %(te)s ' + \ '-overwrite -r %(rsmp_alg)s -dstnodata 255 ' + \ '-wo INIT_DEST=NO_DATA -wo NUM_THREADS=ALL_CPUS ' + \ '%(tiffopt)s %(fname)s %(oname)s' ) % dict( fname=vrtname, oname=oname, tiffopt=tiffopt, prj=prj, res=res, te=te, rsmp_alg = self.rsmp_alg) if not dryrun: if len(cmd) > 255: cmd_x = cmd[:255] + ' ...' else: cmd_x = cmd print('cmd: ' + cmd_x) subprocess.run(shlex.split(cmd), check=True) onames.append(oname) return onames, ds_skelton
"""Reproject a shapefile""" import ogr import osr import os import shutil # Source and target file names srcName = "NYC_MUSEUMS_LAMBERT.shp" tgtName = "NYC_MUSEUMS_GEO.shp" # Target spatial reference tgt_spatRef = osr.SpatialReference() tgt_spatRef.ImportFromEPSG(4326) # Source shapefile driver = ogr.GetDriverByName("ESRI Shapefile") src = driver.Open(srcName, 0) srcLyr = src.GetLayer() # Source spatial reference src_spatRef = srcLyr.GetSpatialRef() # Target shapefile - # delete if it's already # there. if os.path.exists(tgtName): driver.DeleteDataSource(tgtName) tgt = driver.CreateDataSource(tgtName) lyrName = os.path.splitext(tgtName)[0] tgtLyr = tgt.CreateLayer(lyrName, geom_type=ogr.wkbPoint)
def extactProduct(src_ds,index_band_hdf,prefix,sizeX,sizeY,bandList,outFileName_Coords,dateString, dateStart, dateEnd, optionalBand = None): subDsName = src_ds.GetSubDatasets()[index_band_hdf][0] subDs = gdal.Open( subDsName, GA_ReadOnly ) driver = gdal.GetDriverByName( 'GTiff' ) numBands = len(bandList) #dataType = subDs.GetRasterBand(1).DataType dataType = GDT_Float32 #prefix = 'MOD07_Retrived_Temperature_profiles_' outFileName = prefix + dateString + '.tif' cpuCount = multiprocessing.cpu_count() filenameList = [] remappedFileList = [] for i in range(numBands): filename_sl = prefix + str(i+1).zfill(2) data_ds = driver.Create(filename_sl+'.tif', sizeX , sizeY, 1, dataType) band = subDs.GetRasterBand(bandList[i]+1).ReadAsArray() scale_factor = float(subDs.GetMetadata()['scale_factor']) offset = float(subDs.GetMetadata()['add_offset']) valid_range = subDs.GetMetadata()['valid_range'] range_start = int(valid_range.split(', ')[0]) range_end = int(valid_range.split(', ')[1]) data_ds.GetRasterBand(1).WriteArray(scale_factor*(np.array(band)-offset)) data_ds = None bandRemapped = filename_sl+'_remapped.tif' filenameList.append(filename_sl) remappedFileList.append(bandRemapped) filenameList.reverse() finishedJobs = 0; activeJobs = 0; joblist = [] workingDir = os.path.dirname(os.path.realpath(__file__)) + '/../' while finishedJobs < numBands: if activeJobs < cpuCount: if len(filenameList) > 0: fn = filenameList.pop() joblist.append(subprocess.Popen([workingDir + '/bin/remap', '-i', fn+'.tif', '-o', fn+'_remapped.tif', '-a', outFileName_Coords, '-q', '-s', '0.05','-n','-9999'], stdout=open(os.devnull, 'wb'))) activeJobs += 1 i = 0 while i < activeJobs: if joblist[i].poll() != None: joblist.pop(i) finishedJobs += 1 activeJobs -= 1 logging.debug('Remapped '+prefix+' Band: %s of %s' % (str(finishedJobs).zfill(2), str(numBands).zfill(2))) i += 1 time.sleep(0.5) remap_ds = gdal.Open(remappedFileList[0], GA_Update) band = remap_ds.GetRasterBand(1).ReadAsArray() dst_ds = driver.Create(outFileName, band.shape[1], band.shape[0] , numBands, dataType) geotransform = remap_ds.GetGeoTransform() #keep corner coordinates if not geotransform is None: coord = [ remap_ds.RasterYSize*geotransform[5]+geotransform[3] ,geotransform[0], geotransform[3],remap_ds.RasterXSize*geotransform[1]+geotransform[0] ] dst_ds.SetGeoTransform(geotransform) srs = osr.SpatialReference() srs.SetWellKnownGeogCS('WGS84') dst_ds.SetProjection(srs.ExportToWkt()) range_start = (range_start-offset)*scale_factor range_end = (range_end-offset)*scale_factor if index_band_hdf == 15: optionalBandMin = np.ma.masked_array(optionalBand, optionalBand == -9999).min() optionalBandMax = np.ma.masked_array(optionalBand, optionalBand == -9999).max() range_start = (0.622 * (6.112*pow(math.e,((17.67*(range_start-273.15))/((range_start-273.15) + 243.5)))) )/( (optionalBandMin) - (0.378 * (6.112*pow(math.e,((17.67*(range_start-273.15)) /((range_start-273.15) + 243.5)))))) range_end = (0.622 * (6.112*pow(math.e,((17.67*(range_end-273.15))/((range_end-273.15) + 243.5)))) )/( (optionalBandMax) - (0.378 * (6.112*pow(math.e,((17.67*(range_end-273.15)) /((range_end-273.15) + 243.5)))))) remap_ds = None for i in range(numBands): remap_ds = gdal.Open(remappedFileList[i], GA_Update) band = remap_ds.GetRasterBand(1).ReadAsArray() dst_ds.GetRasterBand(numBands-i).SetNoDataValue(-9999) #Surface_Temperature if index_band_hdf == 7: band[band<0]=-9999 dst_ds.GetRasterBand(numBands-i).WriteArray(band) #Surface_Pressure elif index_band_hdf == 8: band[band<0]=-9999 dst_ds.GetRasterBand(numBands-i).WriteArray(band) #Retrieved_Temperature_Profile elif index_band_hdf == 14: band[band<0]=-9999 dst_ds.GetRasterBand(numBands-i).WriteArray(band) #Retrieved_Moisture_Profile if index_band_hdf == 15: #optionalBand = temperature_profile #band = (0.622 * (6.112*pow(math.e,((17.67*(band-273.15))/((band-273.15) + 243.5)))) )/( (optionalBand) - (0.378 * (6.112*pow(math.e,((17.67*(band-273.15)) /((band-273.15) + 243.5)))))) band = 100*(pow(math.e,(17.625*(band-273.15))/(243.04+(band-273.15)))/pow(math.e,(17.625*(optionalBand-273.15))/(243.04+(optionalBand-273.15)))) #TODO: using masked arrays is the more elegant way band[band<0]=-9999 band[band>200]=-9999 dst_ds.GetRasterBand(numBands-i).WriteArray(band) dst_ds.GetRasterBand(numBands-i).ComputeStatistics(False) band = None remap_ds = None #remove temporary files os.system('rm '+ prefix + str(i+1).zfill(2)+'.*') os.system('rm '+ prefix + str(i+1).zfill(2)+'_*') dst_ds.SetMetadataItem('GLOBAL_MIN', str(range_start)) dst_ds.SetMetadataItem('GLOBAL_MAX', str(range_end)) heightLevels = getAltitude() levels = str(heightLevels).replace(' ', '')[1:-1] dst_ds.SetMetadataItem('VERTICAL_LEVELS_NUMBER', str(len(heightLevels))) dst_ds.SetMetadataItem('VERTICAL_LEVELS', str(levels)) dst_ds.SetMetadataItem('TIME_START', dateStart) dst_ds.SetMetadataItem('TIME_END', dateEnd) del dst_ds return outFileName
def test_ogr2ogr_29(): if test_cli_utilities.get_ogr2ogr_path() is None: return 'skip' if ogrtest.have_geos() is 0: return 'skip' for i in range(2): try: os.stat('tmp/wrapdateline_src.shp') ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource( 'tmp/wrapdateline_src.shp') except: pass try: os.stat('tmp/wrapdateline_dst.shp') ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource( 'tmp/wrapdateline_dst.shp') except: pass ds = ogr.GetDriverByName('ESRI Shapefile').CreateDataSource( 'tmp/wrapdateline_src.shp') srs = osr.SpatialReference() srs.ImportFromEPSG(4326) lyr = ds.CreateLayer('wrapdateline_src', srs=srs) feat = ogr.Feature(lyr.GetLayerDefn()) if i == 0: geom = ogr.CreateGeometryFromWkt( 'POLYGON((179 40,179.5 40,-179.5 40,-179 40,-170 40,-165 40,-165 30,-170 30,-179 30,-179.5 30,179.5 30,179 30,179 40))' ) else: geom = ogr.CreateGeometryFromWkt( 'POLYGON((-165 30,-170 30,-179 30,-179.5 30,179.5 30,179 30,179 40,179.5 40,-179.5 40,-179 40,-170 40,-165 40,-165 30))' ) feat.SetGeometry(geom) lyr.CreateFeature(feat) feat.Destroy() ds.Destroy() gdaltest.runexternal( test_cli_utilities.get_ogr2ogr_path() + ' -wrapdateline tmp/wrapdateline_dst.shp tmp/wrapdateline_src.shp') expected_wkt = 'MULTIPOLYGON (((180 30,179.5 30.0,179 30,179 40,179.5 40.0,180 40,180 30)),((-180 40,-179.5 40.0,-179 40,-170 40,-165 40,-165 30,-170 30,-179 30,-179.5 30.0,-180 30,-180 40)))' expected_geom = ogr.CreateGeometryFromWkt(expected_wkt) ds = ogr.Open('tmp/wrapdateline_dst.shp') lyr = ds.GetLayer(0) feat = lyr.GetNextFeature() ret = ogrtest.check_feature_geometry(feat, expected_geom) if ret != 0: print('src is : %s' % geom.ExportToWkt()) print('got : %s' % feat.GetGeometryRef().ExportToWkt()) feat.Destroy() expected_geom.Destroy() ds.Destroy() ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource( 'tmp/wrapdateline_src.shp') ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource( 'tmp/wrapdateline_dst.shp') if ret != 0: return 'fail' return 'success'
def agg_quota(csv_file, raster): ### Aggiungo la quota ai .csv # Apro il csv e lo leggo con DictReader csvfile_read = open(csv_file) reader = csv.DictReader(csvfile_read, delimiter=',') # Apro il dem, ne estraggo la GeoTransform e la banda 1 dem = gdal.Open(raster) gt = dem.GetGeoTransform() band = dem.GetRasterBand(1) # Definisco i campi del nuovo csv fields = [ 'COD_REG', 'COD_CM', 'COD_PRO', 'PRO_COM', 'COMUNE', 'NOME_TED', 'FLAG_CM', 'SHAPE_Leng', 'SHAPE_Area', 'xcoord', 'ycoord', 'Quota' ] # Creo la lista vuota che poi scriverò nel nuovo csv lista = [] # Uso un ciclo for per il nuovo file che sia uguale al precedente ma con in più la quota for row in reader: # Creo la lista della riga da creare a ogni ciclo list_row = [] # Prendo le coordinate x e y dal csv utm_x = float(row['xcoord']) utm_y = float(row['ycoord']) # Calcolo le coordinate (x e y) pixel riferite alla coordinate prese dal csv px = int((utm_x - gt[0]) / gt[1]) py = int((utm_y - gt[3]) / gt[5]) # Leggo la banda come un array e prendo in considerazione il pixel che mi serve, poi lo trasformo in intero quota = band.ReadAsArray(px, py, 1, 1) # print(quota) q = int(quota) # print(q) # Aggiungo a «list_row» tutti gli elementi della riga per poter formare una lista list_row.append(row['COD_REG']) list_row.append(row['COD_CM']) list_row.append(row['COD_PRO']) list_row.append(row['PRO_COM']) list_row.append(row['COMUNE']) list_row.append(row['NOME_TED']) list_row.append(row['FLAG_CM']) list_row.append(row['SHAPE_Leng']) list_row.append(row['SHAPE_Area']) list_row.append(row['xcoord']) list_row.append(row['ycoord']) list_row.append(q) # Aggiungo «list_row» a lista per formare una lista di liste da scrivere nel csv lista.append(list_row) # Scrivo la lista di liste e l'header nel csv # Definisco il nome del nuovo file aggiungendo una parte al nome di quello vecchio name = csv_file.split('.')[0] + '_q.csv' csvfile_write = open(name, 'w') writer = csv.writer(csvfile_write) writer.writerow(fields) writer.writerows(lista) # Chiudo i file csvfile_read.close() csvfile_write.close() ### Trasformo i .csv in .shp # Creo il nuovo shapefile definendone il driver e il datasource driver = ogr.GetDriverByName('ESRI Shapefile') shapefile = driver.CreateDataSource( name.split('.')[0] + '_punti_quotati.shp') # Apro e leggo il file csv con DictReader csvfile = open(name) reader = csv.DictReader(csvfile, delimiter=',') # Definisco il sistema di riferimento che poi inserirò nel layer SR = osr.SpatialReference() SR.ImportFromEPSG(32632) # Creo il layer definendo nome, sistema di riferimento e tipologia di geometria layer = shapefile.CreateLayer( name.split('.')[0] + '_punti_quotati.shp', SR, ogr.wkbPoint) # Definisco nome e tipologia dei vari campi della tabella attributi e li creo layer.CreateField(ogr.FieldDefn('Cod_reg', ogr.OFTInteger)) layer.CreateField(ogr.FieldDefn('Cod_cm', ogr.OFTInteger)) layer.CreateField(ogr.FieldDefn('Cod_pro', ogr.OFTInteger)) layer.CreateField(ogr.FieldDefn('Pro_com', ogr.OFTInteger)) name_field = ogr.FieldDefn('Comune', ogr.OFTString) name_field.SetWidth(34) # Definisco lunghezza massima della stringa layer.CreateField(name_field) layer.CreateField(ogr.FieldDefn('Nome_Ted', ogr.OFTInteger)) layer.CreateField(ogr.FieldDefn('Flag_cm', ogr.OFTInteger)) layer.CreateField(ogr.FieldDefn('Shape_Leng', ogr.OFTReal)) layer.CreateField(ogr.FieldDefn('Shape_Area', ogr.OFTReal)) name_field_1 = ogr.FieldDefn('xcoord', ogr.OFTReal) layer.CreateField(name_field_1) layer.CreateField(ogr.FieldDefn('ycoord', ogr.OFTReal)) layer.CreateField(ogr.FieldDefn('Quota', ogr.OFTInteger)) # Faccio un ciclo for per scrivere ogni riga del csv nella tabella attributi del layer for row in reader: # Estraggo la definizione del layer layer_defn = layer.GetLayerDefn() # Definisco la feature all'interno della definizione dal layer # e ne imposto il valore nei vari campi della tabella degli attributi feature = ogr.Feature(layer_defn) feature.SetField('Cod_reg', row['COD_REG']) feature.SetField('Cod_cm', row['COD_CM']) feature.SetField('Cod_pro', row['COD_PRO']) feature.SetField('Pro_com', row['PRO_COM']) feature.SetField('Comune', row['COMUNE']) feature.SetField('Nome_Ted', row['NOME_TED']) feature.SetField('Flag_cm', row['FLAG_CM']) feature.SetField('Shape_Leng', row['SHAPE_Leng']) feature.SetField('Shape_Area', row['SHAPE_Area']) feature.SetField('xcoord', row['xcoord']) feature.SetField('ycoord', row['ycoord']) feature.SetField('Quota', row['Quota']) # Scrivo la geometria in linguaggio wkt wkt = "POINT(%f %f)" % (float(row['xcoord']), float(row['ycoord'])) # Creo la geometria dalla sringa scritta in wkt point = ogr.CreateGeometryFromWkt(wkt) # Assegno la geometria creata alla feature feature.SetGeometry(point) # Creo la feature (con attributi e geometria) all'interno del layer layer.CreateFeature(feature) # Svuoto la variabile feature e faccio ripartire il ciclo for feature = None # Chiudo e salvo il datasource shapefile = None
def ogr_pcidsk_2(): ogr_drv = ogr.GetDriverByName('PCIDSK') if ogr_drv is None: return 'skip' ds = ogr.Open('tmp/ogr_pcidsk_1.pix') if ds.GetLayerCount() != 2 + len(wkts): return 'fail' lyr = ds.GetLayerByName('nothing') if lyr.GetGeomType() != ogr.wkbNone: gdaltest.post_reason('failure') return 'fail' feat = lyr.GetNextFeature() if feat is None: gdaltest.post_reason('failure') return 'fail' lyr = ds.GetLayerByName('fields') feat = lyr.GetNextFeature() if feat is None: gdaltest.post_reason('failure') return 'fail' if feat.GetField(0) != 'foo': gdaltest.post_reason('failure') return 'fail' if feat.GetField(1) != 1: gdaltest.post_reason('failure') return 'fail' if feat.GetField(2) != 3.45: gdaltest.post_reason('failure') return 'fail' for (wkt, layername, epsgcode) in wkts: geom = ogr.CreateGeometryFromWkt(wkt) lyr = ds.GetLayerByName(layername) if lyr.GetGeomType() != geom.GetGeometryType(): gdaltest.post_reason('failure') print(layername) return 'fail' srs = lyr.GetSpatialRef() if epsgcode != 0: ref_srs = osr.SpatialReference() ref_srs.ImportFromEPSG(epsgcode) if srs is None or ref_srs.IsSame(srs) != 1: gdaltest.post_reason('failure') print(layername) print(ref_srs) print(srs) return 'fail' feat = lyr.GetNextFeature() if feat is None: gdaltest.post_reason('failure') print(layername) return 'fail' if feat.GetGeometryRef().ExportToWkt() != wkt: gdaltest.post_reason('failure') feat.DumpReadable() print(layername) return 'fail' ds = None return 'success'
#Resize Images to receive texture and define filenames contrast = im_resize(plt_cont,Nx,Ny) contrast[merge==0]=np.nan dissimilarity = im_resize(plt_diss,Nx,Ny) dissimilarity[merge==0]=np.nan homogeneity = im_resize(plt_homo,Nx,Ny) homogeneity[merge==0]=np.nan energy = im_resize(plt_eng,Nx,Ny) energy[merge==0]=np.nan correlation = im_resize(plt_corr,Nx,Ny) correlation[merge==0]=np.nan ASM = im_resize(plt_ASM,Nx,Ny) ASM[merge==0]=np.nan del plt_cont, plt_diss, plt_homo, plt_eng, plt_corr, plt_ASM del w,Z,ind,Ny,Nx driverName= 'GTiff' epsg_code=26949 proj = osr.SpatialReference() proj.ImportFromEPSG(epsg_code) CreateRaster(xx, yy, contrast, gt, proj,driverName,contFile) CreateRaster(xx, yy, dissimilarity, gt, proj,driverName,dissFile) CreateRaster(xx, yy, homogeneity, gt, proj,driverName,homoFile) CreateRaster(xx, yy, energy, gt, proj,driverName,energyFile) CreateRaster(xx, yy, correlation, gt, proj,driverName,corrFile) CreateRaster(xx, yy, ASM, gt, proj,driverName,ASMFile) del contrast, merge, xx, yy, gt, meter, dissimilarity, homogeneity, energy, correlation, ASM
def _runGeocodeFrequency(self, frequency): self._print(f'starting geocode module for frequency: {frequency}') state = self.state pol_list = state.subset_dict[frequency] radar_grid = self.radar_grid_list[frequency] orbit = self.orbit raster_ref_list = [] for pol in pol_list: h5_ds = f'//science/LSAR/SLC/swaths/frequency{frequency}/{pol}' raster_ref = f'HDF5:"{state.input_hdf5}":{h5_ds}' raster_ref_list.append(raster_ref) self._print('raster list:', raster_ref_list) self._print('pol list: ', pol_list) # set temporary files time_id = str(time.time()) input_temp = os.path.join(state.scratch_path, f'temp_rslc2gcov_{frequency}_{time_id}.vrt') output_file = os.path.join(state.scratch_path, f'temp_rslc2gcov_{frequency}_{time_id}.bin') out_geo_nlooks = os.path.join(state.scratch_path, f'temp_geo_nlooks_{time_id}.bin') out_geo_rtc = os.path.join(state.scratch_path, f'temp_geo_rtc_{time_id}.bin') out_dem_vertices = os.path.join(state.scratch_path, f'temp_dem_vertices_{time_id}.bin') out_geo_vertices = os.path.join(state.scratch_path, f'temp_geo_vertices_{time_id}.bin') # build input VRT gdal.BuildVRT(input_temp, raster_ref_list, separate=True) input_raster_obj = isce3.pyRaster(input_temp) ellps = isce3.pyEllipsoid() # RTC rtc_dict = self.get_value(['processing', 'rtc']) rtc_output_type = rtc_dict['output_type'] rtc_geogrid_upsampling = rtc_dict['geogrid_upsampling'] rtc_algorithm_type = rtc_dict['algorithm_type'] input_terrain_radiometry = rtc_dict[ 'input_terrain_radiometry'] rtc_min_value_db = rtc_dict['rtc_min_value_db'] # Geocode geocode_dict = self.get_value(['processing', 'geocode']) geocode_algorithm_type = geocode_dict['algorithm_type'] memory_mode = geocode_dict['memory_mode'] geogrid_upsampling = geocode_dict['geogrid_upsampling'] abs_cal_factor = geocode_dict['abs_rad_cal'] clip_min = geocode_dict['clip_min'] clip_max = geocode_dict['clip_max'] min_nlooks = geocode_dict['min_nlooks'] flag_save_nlooks = geocode_dict['save_nlooks'] flag_save_rtc = geocode_dict['save_rtc'] flag_save_dem_vertices = geocode_dict['save_dem_vertices'] flag_save_geo_vertices = geocode_dict['save_geo_vertices'] # Geogrid state.output_epsg = geocode_dict['outputEPSG'] y_snap = geocode_dict['y_snap'] x_snap = geocode_dict['x_snap'] y_max = geocode_dict['top_left']['y_abs'] x_min = geocode_dict['top_left']['x_abs'] y_min = geocode_dict['bottom_right']['y_abs'] x_max = geocode_dict['bottom_right']['x_abs'] step = geocode_dict['output_posting'] # fix types rtc_min_value_db = self.cast_input(rtc_min_value_db, dtype=float, frequency=frequency) state.output_epsg = self.cast_input(state.output_epsg, dtype=int, frequency=frequency) geogrid_upsampling = self.cast_input(geogrid_upsampling, dtype=float, frequency=frequency) rtc_geogrid_upsampling = self.cast_input(rtc_geogrid_upsampling, dtype=float, frequency=frequency) abs_cal_factor = self.cast_input(abs_cal_factor, dtype=float, frequency=frequency) clip_min = self.cast_input(clip_min, dtype=float, default=0, frequency=frequency) clip_max = self.cast_input(clip_max, dtype=float, default=2, frequency=frequency) min_nlooks = self.cast_input(min_nlooks, dtype=float, frequency=frequency) y_snap = self.cast_input(y_snap, dtype=float, default=np.nan, frequency=frequency) x_snap = self.cast_input(x_snap, dtype=float, default=np.nan, frequency=frequency) y_max = self.cast_input(y_max, dtype=float, default=np.nan, frequency=frequency) x_min = self.cast_input(x_min, dtype=float, default=np.nan, frequency=frequency) y_min = self.cast_input(y_min, dtype=float, default=np.nan, frequency=frequency) x_max = self.cast_input(x_max, dtype=float, default=np.nan, frequency=frequency) step_x = self.cast_input(step, dtype=float, default=np.nan, frequency=frequency) step_y = -step_x if _is_valid(step_x) else None # prepare parameters zero_doppler = isce3.pyLUT2d() # Instantiate Geocode object depending on raster type if input_raster_obj.getDatatype() == gdal.GDT_Float32: geo = isce3.pyGeocodeFloat(orbit, ellps) elif input_raster_obj.getDatatype() == gdal.GDT_Float64: geo = isce3.pyGeocodeDouble(orbit, ellps) elif input_raster_obj.getDatatype() == gdal.GDT_CFloat32: geo = isce3.pyGeocodeComplexFloat(orbit, ellps) elif input_raster_obj.getDatatype() == gdal.GDT_CFloat64: geo = isce3.pyGeocodeComplexDouble(orbit, ellps) else: raise NotImplementedError('Unsupported raster type for geocoding') dem_raster = isce3.pyRaster(state.dem_file) if state.output_epsg is None: state.output_epsg = dem_raster.EPSG if state.geotransform_dict is None: state.geotransform_dict = {} if (_is_valid(y_min) and _is_valid(y_max) and _is_valid(step_y)): size_y = int(np.round((y_min - y_max)/step_y)) else: size_y = -32768 if (_is_valid(x_max) and _is_valid(x_min) and _is_valid(step_x)): size_x = int(np.round((x_max - x_min)/step_x)) else: size_x = -32768 # if Geogrid is not fully determined, let Geocode find the missing values if (size_x == -32768 or size_y == -32768): geo.geoGrid(x_min, y_max, step_x, step_y, size_x, size_y, state.output_epsg) geo.updateGeoGrid(radar_grid, dem_raster) # update only missing values if not _is_valid(x_min): x_min = geo.geoGridStartX if not _is_valid(y_max): y_max = geo.geoGridStartY if not _is_valid(step_x): step_x = geo.geoGridSpacingX if not _is_valid(step_y): step_y = geo.geoGridSpacingY if not _is_valid(x_max): x_max = geo.geoGridStartX + geo.geoGridSpacingX * geo.geoGridWidth if not _is_valid(y_min): y_min = geo.geoGridStartY + geo.geoGridSpacingY * geo.geoGridLength x_min = _snap_coordinate(x_min, x_snap, np.floor) y_max = _snap_coordinate(y_max, y_snap, np.ceil) x_max = _snap_coordinate(x_max, x_snap, np.ceil) y_min = _snap_coordinate(y_min, y_snap, np.floor) size_y = int(np.round((y_min - y_max)/step_y)) size_x = int(np.round((x_max - x_min)/step_x)) geo.geoGrid(x_min, y_max, step_x, step_y, size_x, size_y, state.output_epsg) output_dir = os.path.dirname(output_file) if output_dir and not os.path.isdir(output_dir): os.makedirs(output_dir) exponent = 2 output_dtype = gdal.GDT_Float32 nbands = input_raster_obj.numBands if geogrid_upsampling is None: geogrid_upsampling = 1 self._print(f'creating temporary output raster: {output_file}') geocoded_dict = defaultdict(lambda: None) output_raster_obj = isce3.pyRaster(output_file, gdal.GA_Update, output_dtype, size_x, size_y, nbands, "ENVI") geocoded_dict['output_file'] = output_file if flag_save_nlooks: out_geo_nlooks_obj = isce3.pyRaster(out_geo_nlooks, gdal.GA_Update, gdal.GDT_Float32, size_x, size_y, 1, "ENVI") geocoded_dict['out_geo_nlooks'] = out_geo_nlooks else: out_geo_nlooks_obj = None if flag_save_rtc: out_geo_rtc_obj = isce3.pyRaster(out_geo_rtc, gdal.GA_Update, gdal.GDT_Float32, size_x, size_y, 1, "ENVI") geocoded_dict['out_geo_rtc'] = out_geo_rtc else: out_geo_rtc_obj = None if flag_save_dem_vertices: out_dem_vertices_obj = isce3.pyRaster(out_dem_vertices, gdal.GA_Update, gdal.GDT_Float32, size_x + 1, size_y + 1, 1, "ENVI") geocoded_dict['out_dem_vertices'] = out_dem_vertices else: out_dem_vertices_obj = None if flag_save_geo_vertices: out_geo_vertices_obj = isce3.pyRaster(out_geo_vertices, gdal.GA_Update, gdal.GDT_Float32, size_x + 1, size_y + 1, 2, "ENVI") geocoded_dict['out_geo_vertices'] = out_geo_vertices else: out_geo_vertices_obj = None # Run geocoding flag_apply_rtc = (rtc_output_type and rtc_output_type != input_terrain_radiometry and 'gamma' in rtc_output_type) if flag_apply_rtc is None: flag_apply_rtc = False geotransform = [x_min, step_x, 0, y_max, 0, step_y] state.geotransform_dict[frequency] = geotransform # output mode if ('interp' in geocode_algorithm_type and flag_apply_rtc): raise NotImplementedError('ERROR interp algorithm does not provide' ' RTC correction') elif 'interp' in geocode_algorithm_type: output_mode = 'interp' elif not flag_apply_rtc: output_mode = 'area-projection' else: output_mode = 'area-projection-gamma_naught' # input terrain radiometry if (input_terrain_radiometry is not None and 'sigma' in input_terrain_radiometry): input_radiometry = 'sigma-naught-ellipsoid' else: input_radiometry = 'beta-naught' if flag_apply_rtc: output_radiometry_str = 'gamma-naught' else: output_radiometry_str = input_radiometry # number of looks radar_grid_nlooks = state.nlooks_az * state.nlooks_rg # rtc min value kwargs = {} if rtc_min_value_db is not None: kwargs['rtc_min_value_db'] = rtc_min_value_db # absolute calibration factor if abs_cal_factor is not None: kwargs['abs_cal_factor'] = abs_cal_factor # memory mode if memory_mode is not None: kwargs['memory_mode'] = memory_mode if (rtc_algorithm_type is not None and ('DAVID' in rtc_algorithm_type.upper() or 'SMALL' in rtc_algorithm_type.upper())): kwargs['rtc_algorithm'] = 'RTC_DAVID_SMALL' elif rtc_algorithm_type is not None: kwargs['rtc_algorithm'] = 'RTC_AREA_PROJECTION' if (rtc_geogrid_upsampling is not None and np.isfinite(rtc_geogrid_upsampling)): kwargs['rtc_upsampling'] = rtc_geogrid_upsampling if clip_min is not None: kwargs['clip_min'] = clip_min if clip_max is not None: kwargs['clip_max'] = clip_max if min_nlooks is not None: kwargs['min_nlooks'] = min_nlooks # call the geocode module geo.geocode(radar_grid, input_raster_obj, output_raster_obj, dem_raster, output_mode=output_mode, upsampling=geogrid_upsampling, input_radiometry=input_radiometry, exponent=exponent, radar_grid_nlooks=radar_grid_nlooks, out_geo_nlooks=out_geo_nlooks_obj, out_geo_rtc=out_geo_rtc_obj, out_dem_vertices=out_dem_vertices_obj, out_geo_vertices=out_geo_vertices_obj, **kwargs) del output_raster_obj if flag_save_nlooks: del out_geo_nlooks_obj if flag_save_rtc: del out_geo_rtc_obj if flag_save_dem_vertices: del out_dem_vertices_obj if flag_save_geo_vertices: del out_geo_vertices_obj self._print(f'removing temporary file: {input_temp}') _remove(input_temp) output_hdf5 = state.output_hdf5 h5_ds_list = [] with h5py.File(output_hdf5, 'a') as hdf5_obj: hdf5_obj.attrs['Conventions'] = np.string_("CF-1.8") root_ds = os.path.join('//', 'science', 'LSAR', 'GCOV', 'grids', f'frequency{frequency}') # radiometricTerrainCorrectionFlag h5_ds = os.path.join(root_ds, 'listOfPolarizations') if h5_ds in hdf5_obj: del hdf5_obj[h5_ds] pol_list_s2 = np.array(pol_list, dtype='S2') dset = hdf5_obj.create_dataset(h5_ds, data=pol_list_s2) h5_ds_list.append(h5_ds) dset.attrs['description'] = np.string_( 'List of processed polarization layers with frequency ' + frequency) h5_ds = os.path.join(root_ds, 'radiometricTerrainCorrectionFlag') if h5_ds in hdf5_obj: del hdf5_obj[h5_ds] dset = hdf5_obj.create_dataset(h5_ds, data=np.string_(str(flag_apply_rtc))) h5_ds_list.append(h5_ds) # X and Y coordinates geotransform = self.state.geotransform_dict[frequency] dx = geotransform[1] dy = geotransform[5] x0 = geotransform[0] + 0.5 * dx y0 = geotransform[3] + 0.5 * dy xf = x0 + (size_x - 1) * dx yf = y0 + (size_y - 1) * dy # xCoordinates h5_ds = os.path.join(root_ds, 'xCoordinates') # float64 x_vect = np.linspace(x0, xf, size_x, dtype=np.float64) if h5_ds in hdf5_obj: del hdf5_obj[h5_ds] xds = hdf5_obj.create_dataset(h5_ds, data=x_vect) h5_ds_list.append(h5_ds) try: xds.make_scale() except AttributeError: pass # yCoordinates h5_ds = os.path.join(root_ds, 'yCoordinates') # float64 y_vect = np.linspace(y0, yf, size_y, dtype=np.float64) if h5_ds in hdf5_obj: del hdf5_obj[h5_ds] yds = hdf5_obj.create_dataset(h5_ds, data=y_vect) h5_ds_list.append(h5_ds) try: yds.make_scale() except AttributeError: pass #Associate grid mapping with data - projection created later h5_ds = os.path.join(root_ds, "projection") #Set up osr for wkt srs = osr.SpatialReference() srs.ImportFromEPSG(self.state.output_epsg) ###Create a new single int dataset for projections if h5_ds in hdf5_obj: del hdf5_obj[h5_ds] projds = hdf5_obj.create_dataset(h5_ds, (), dtype='i') projds[()] = self.state.output_epsg h5_ds_list.append(h5_ds) ##WGS84 ellipsoid projds.attrs['semi_major_axis'] = 6378137.0 projds.attrs['inverse_flattening'] = 298.257223563 projds.attrs['ellipsoid'] = np.string_("WGS84") ##Additional fields projds.attrs['epsg_code'] = self.state.output_epsg ##CF 1.7+ requires this attribute to be named "crs_wkt" ##spatial_ref is old GDAL way. Using that for testing only. ##For NISAR replace with "crs_wkt" projds.attrs['spatial_ref'] = np.string_(srs.ExportToWkt()) ##Here we have handcoded the attributes for the different cases ##Recommended method is to use pyproj.CRS.to_cf() as shown above ##To get complete set of attributes. ###Geodetic latitude / longitude if self.state.output_epsg == 4326: #Set up grid mapping projds.attrs['grid_mapping_name'] = np.string_('latitude_longitude') projds.attrs['longitude_of_prime_meridian'] = 0.0 #Setup units for x and y xds.attrs['standard_name'] = np.string_("longitude") xds.attrs['units'] = np.string_("degrees_east") yds.attrs['standard_name'] = np.string_("latitude") yds.attrs['units'] = np.string_("degrees_north") ### UTM zones elif ((self.state.output_epsg > 32600 and self.state.output_epsg < 32661) or (self.state.output_epsg > 32700 and self.state.output_epsg < 32761)): #Set up grid mapping projds.attrs['grid_mapping_name'] = np.string_('universal_transverse_mercator') projds.attrs['utm_zone_number'] = self.state.output_epsg % 100 #Setup units for x and y xds.attrs['standard_name'] = np.string_("projection_x_coordinate") xds.attrs['long_name'] = np.string_("x coordinate of projection") xds.attrs['units'] = np.string_("m") yds.attrs['standard_name'] = np.string_("projection_y_coordinate") yds.attrs['long_name'] = np.string_("y coordinate of projection") yds.attrs['units'] = np.string_("m") ### Polar Stereo North elif self.state.output_epsg == 3413: #Set up grid mapping projds.attrs['grid_mapping_name'] = np.string_("polar_stereographic") projds.attrs['latitude_of_projection_origin'] = 90.0 projds.attrs['standard_parallel'] = 70.0 projds.attrs['straight_vertical_longitude_from_pole'] = -45.0 projds.attrs['false_easting'] = 0.0 projds.attrs['false_northing'] = 0.0 #Setup units for x and y xds.attrs['standard_name'] = np.string_("projection_x_coordinate") xds.attrs['long_name'] = np.string_("x coordinate of projection") xds.attrs['units'] = np.string_("m") yds.attrs['standard_name'] = np.string_("projection_y_coordinate") yds.attrs['long_name'] = np.string_("y coordinate of projection") yds.attrs['units'] = np.string_("m") ### Polar Stereo south elif self.state.output_epsg == 3031: #Set up grid mapping projds.attrs['grid_mapping_name'] = np.string_("polar_stereographic") projds.attrs['latitude_of_projection_origin'] = -90.0 projds.attrs['standard_parallel'] = -71.0 projds.attrs['straight_vertical_longitude_from_pole'] = 0.0 projds.attrs['false_easting'] = 0.0 projds.attrs['false_northing'] = 0.0 #Setup units for x and y xds.attrs['standard_name'] = np.string_("projection_x_coordinate") xds.attrs['long_name'] = np.string_("x coordinate of projection") xds.attrs['units'] = np.string_("m") yds.attrs['standard_name'] = np.string_("projection_y_coordinate") yds.attrs['long_name'] = np.string_("y coordinate of projection") yds.attrs['units'] = np.string_("m") ### EASE 2 for soil moisture L3 elif self.state.output_epsg == 6933: #Set up grid mapping projds.attrs['grid_mapping_name'] = np.string_("lambert_cylindrical_equal_area") projds.attrs['longitude_of_central_meridian'] = 0.0 projds.attrs['standard_parallel'] = 30.0 projds.attrs['false_easting'] = 0.0 projds.attrs['false_northing'] = 0.0 #Setup units for x and y xds.attrs['standard_name'] = np.string_("projection_x_coordinate") xds.attrs['long_name'] = np.string_("x coordinate of projection") xds.attrs['units'] = np.string_("m") yds.attrs['standard_name'] = np.string_("projection_y_coordinate") yds.attrs['long_name'] = np.string_("y coordinate of projection") yds.attrs['units'] = np.string_("m") ### Europe Equal Area for Deformation map (to be implemented in isce3) elif self.state.output_epsg == 3035: #Set up grid mapping projds.attrs['grid_mapping_name'] = np.string_("lambert_azimuthal_equal_area") projds.attrs['longitude_of_projection_origin']= 10.0 projds.attrs['latitude_of_projection_origin'] = 52.0 projds.attrs['standard_parallel'] = -71.0 projds.attrs['straight_vertical_longitude_from_pole'] = 0.0 projds.attrs['false_easting'] = 4321000.0 projds.attrs['false_northing'] = 3210000.0 #Setup units for x and y xds.attrs['standard_name'] = np.string_("projection_x_coordinate") xds.attrs['long_name'] = np.string_("x coordinate of projection") xds.attrs['units'] = np.string_("m") yds.attrs['standard_name'] = np.string_("projection_y_coordinate") yds.attrs['long_name'] = np.string_("y coordinate of projection") yds.attrs['units'] = np.string_("m") else: raise NotImplementedError('Waiting for implementation / Not supported in ISCE3') # save GCOV diagonal elements cov_elements_list = [p.upper()+p.upper() for p in pol_list] _save_hdf5_dataset(self, 'output_file', hdf5_obj, root_ds, h5_ds_list, geocoded_dict, frequency, yds, xds, cov_elements_list, standard_name = output_radiometry_str, long_name = output_radiometry_str, units = 'unitless', fill_value = np.nan, valid_min = clip_min, valid_max = clip_max) # save nlooks _save_hdf5_dataset(self, 'out_geo_nlooks', hdf5_obj, root_ds, h5_ds_list, geocoded_dict, frequency, yds, xds, 'numberOfLooks', standard_name = 'numberOfLooks', long_name = 'number of looks', units = 'looks', fill_value = np.nan, valid_min = 0) # save rtc if flag_apply_rtc: _save_hdf5_dataset(self, 'out_geo_rtc', hdf5_obj, root_ds, h5_ds_list, geocoded_dict, frequency, yds, xds, 'areaNormalizationFactor', standard_name = 'areaNormalizationFactor', long_name = 'RTC area factor', units = 'unitless', fill_value = np.nan, valid_min = 0, valid_max = 2) if ('out_dem_vertices' in geocoded_dict or 'out_geo_vertices' in geocoded_dict): # X and Y coordinates geotransform = self.state.geotransform_dict[frequency] dx = geotransform[1] dy = geotransform[5] x0 = geotransform[0] y0 = geotransform[3] xf = xf + size_x * dx yf = yf + size_y * dy # xCoordinates h5_ds = os.path.join(root_ds, 'xCoordinatesVertices') # float64 x_vect_vertices = np.linspace(x0, xf, size_x + 1, dtype=np.float64) if h5_ds in hdf5_obj: del hdf5_obj[h5_ds] xds_vertices = hdf5_obj.create_dataset(h5_ds, data=x_vect_vertices) h5_ds_list.append(h5_ds) try: xds_vertices.make_scale() except AttributeError: pass # yCoordinates h5_ds = os.path.join(root_ds, 'yCoordinatesVertices') # float64 y_vect_vertices = np.linspace(y0, yf, size_y + 1, dtype=np.float64) if h5_ds in hdf5_obj: del hdf5_obj[h5_ds] yds_vertices = hdf5_obj.create_dataset(h5_ds, data=y_vect_vertices) h5_ds_list.append(h5_ds) try: yds_vertices.make_scale() except AttributeError: pass # save geo grid _save_hdf5_dataset(self, 'out_dem_vertices', hdf5_obj, root_ds, h5_ds_list, geocoded_dict, frequency, yds_vertices, xds_vertices, 'interpolatedDem', standard_name = 'interpolatedDem', long_name = 'interpolated dem', units = 'meters', fill_value = np.nan, valid_min = -500, valid_max = 9000) # save geo vertices _save_hdf5_dataset(self, 'out_geo_vertices', hdf5_obj, root_ds, h5_ds_list, geocoded_dict, frequency, yds_vertices, xds_vertices, ['vertices_a', 'vertices_r']) for h5_ds_str in h5_ds_list: h5_ref = f'HDF5:{output_hdf5}:{h5_ds_str}' state.outputList[frequency].append(h5_ref)
def _get_raster_srs(raster): srs_wkt = raster.GetProjection() srs = osr.SpatialReference() srs.ImportFromWkt(srs_wkt) return srs
def run(self): if self.host: if not self.connect(): log("Connection to host failed", Error) return if not self.get_file(self.raster_file): log("Failed to download file", Error) return else: self.real_file_name = self.raster_file #log("Hello its me", Standard) dataset = gdal.Open(self.real_file_name, GA_ReadOnly) if not dataset: log("Failed to open file", Error) self.setStatus(MOD_EXECUTION_ERROR) return band = dataset.GetRasterBand(1) gt = dataset.GetGeoTransform() srs = osr.SpatialReference() srs.ImportFromWkt(dataset.GetProjection()) srsLatLong = osr.SpatialReference() srsLatLong.ImportFromEPSG( self.getSimulationConfig().getCoorindateSystem()) ct = osr.CoordinateTransformation(srsLatLong, srs) inMemory = True if inMemory: values = band.ReadAsArray(0, 0, band.XSize, band.YSize) for node in self.node_view: geom = node.GetGeometryRef() env = geom.GetEnvelope() p1 = ct.TransformPoint(env[0], env[2]) p2 = ct.TransformPoint(env[1], env[3]) minx = int((p1[0] - gt[0]) / gt[1]) miny = int((p1[1] - gt[3]) / gt[5]) maxx = int((p2[0] - gt[0]) / gt[1]) maxy = int((p2[1] - gt[3]) / gt[5]) #print env print gt print minx, miny, maxx, maxy if miny > maxy: min_y_tmp = miny miny = maxy maxy = min_y_tmp #print minx, miny, maxx, maxy datatype = band.DataType sum_val = 0 val_array = np.zeros(16) for x in range(minx, maxx + 1): for y in range(miny, maxy + 1): if inMemory: if x < 0 or y < 0 or x > band.XSize - 1 or y > band.YSize - 1: continue idx = int(values[int(y)][int(x)]) if idx < 0 or idx > 15: continue val_array[idx] += 1 for key in self.landuse_classes: if val_array.sum() < 1: continue node.SetField( key, float(val_array[self.landuse_classes[key]] / val_array.sum())) print("syncronise") self.node_view.finalise()
def _get_srs_from_epsg_code(code): srs = osr.SpatialReference() srs.ImportFromEPSG(code) return srs
for line in fp: item = line.split() if len(item) < 2: continue if item[0][0] == '#': continue ang = float(item[1]) dif = np.abs(incidence_angle - ang) indx = np.argmin(dif) if dif[indx] > 0.1: raise ValueError('Error, dif={}'.format(dif[indx])) incidence_indx.update({item[0]: indx}) ds = gdal.Open(input_fnam) prj = ds.GetProjection() srs = osr.SpatialReference(wkt=prj) data = ds.ReadAsArray() trans = ds.GetGeoTransform( ) # maybe obtained from tif_tags['ModelTransformationTag'] indy, indx = np.indices(data[0].shape) if srs.IsProjected(): pnam = srs.GetAttrValue('projcs') if re.search('UTM', pnam): xp = trans[0] + (indx + 0.5) * trans[1] + (indy + 0.5) * trans[2] yp = trans[3] + (indx + 0.5) * trans[4] + (indy + 0.5) * trans[5] else: raise ValueError('Unsupported projection >>> ' + pnam) else: lon = trans[0] + (indx + 0.5) * trans[1] + (indy + 0.5) * trans[2] lat = trans[3] + (indx + 0.5) * trans[4] + (indy + 0.5) * trans[5] xp, yp, zp = transform_wgs84_to_utm(lon, lat)
def __init__(self, filename=None): Base.__init__(self, filename) # set default spatial reference sr = osr.SpatialReference() sr.ImportFromEPSG(4326) self.wgs84 = sr.ExportToWkt()
def osr_xml_1(): gdaltest.srs_xml = """<gml:ProjectedCRS gml:id="ogrcrs1"> <gml:srsName>WGS 84 / UTM zone 31N</gml:srsName> <gml:srsID> <gml:name gml:codeSpace="urn:ogc:def:crs:EPSG::">32631</gml:name> </gml:srsID> <gml:baseCRS> <gml:GeographicCRS gml:id="ogrcrs2"> <gml:srsName>WGS 84</gml:srsName> <gml:srsID> <gml:name gml:codeSpace="urn:ogc:def:crs:EPSG::">4326</gml:name> </gml:srsID> <gml:usesEllipsoidalCS> <gml:EllipsoidalCS gml:id="ogrcrs3"> <gml:csName>ellipsoidal</gml:csName> <gml:csID> <gml:name gml:codeSpace="urn:ogc:def:cs:EPSG::">6402</gml:name> </gml:csID> <gml:usesAxis> <gml:CoordinateSystemAxis gml:id="ogrcrs4" gml:uom="urn:ogc:def:uom:EPSG::9102"> <gml:name>Geodetic latitude</gml:name> <gml:axisID> <gml:name gml:codeSpace="urn:ogc:def:axis:EPSG::">9901</gml:name> </gml:axisID> <gml:axisAbbrev>Lat</gml:axisAbbrev> <gml:axisDirection>north</gml:axisDirection> </gml:CoordinateSystemAxis> </gml:usesAxis> <gml:usesAxis> <gml:CoordinateSystemAxis gml:id="ogrcrs5" gml:uom="urn:ogc:def:uom:EPSG::9102"> <gml:name>Geodetic longitude</gml:name> <gml:axisID> <gml:name gml:codeSpace="urn:ogc:def:axis:EPSG::">9902</gml:name> </gml:axisID> <gml:axisAbbrev>Lon</gml:axisAbbrev> <gml:axisDirection>east</gml:axisDirection> </gml:CoordinateSystemAxis> </gml:usesAxis> </gml:EllipsoidalCS> </gml:usesEllipsoidalCS> <gml:usesGeodeticDatum> <gml:GeodeticDatum gml:id="ogrcrs6"> <gml:datumName>WGS_1984</gml:datumName> <gml:datumID> <gml:name gml:codeSpace="urn:ogc:def:datum:EPSG::">6326</gml:name> </gml:datumID> <gml:usesPrimeMeridian> <gml:PrimeMeridian gml:id="ogrcrs7"> <gml:meridianName>Greenwich</gml:meridianName> <gml:meridianID> <gml:name gml:codeSpace="urn:ogc:def:meridian:EPSG::">8901</gml:name> </gml:meridianID> <gml:greenwichLongitude> <gml:angle gml:uom="urn:ogc:def:uom:EPSG::9102">0</gml:angle> </gml:greenwichLongitude> </gml:PrimeMeridian> </gml:usesPrimeMeridian> <gml:usesEllipsoid> <gml:Ellipsoid gml:id="ogrcrs8"> <gml:ellipsoidName>WGS 84</gml:ellipsoidName> <gml:ellipsoidID> <gml:name gml:codeSpace="urn:ogc:def:ellipsoid:EPSG::">7030</gml:name> </gml:ellipsoidID> <gml:semiMajorAxis gml:uom="urn:ogc:def:uom:EPSG::9001">6378137</gml:semiMajorAxis> <gml:secondDefiningParameter> <gml:inverseFlattening gml:uom="urn:ogc:def:uom:EPSG::9201">298.257223563</gml:inverseFlattening> </gml:secondDefiningParameter> </gml:Ellipsoid> </gml:usesEllipsoid> </gml:GeodeticDatum> </gml:usesGeodeticDatum> </gml:GeographicCRS> </gml:baseCRS> <gml:definedByConversion> <gml:Conversion gml:id="ogrcrs9"> <gml:usesMethod xlink:href="urn:ogc:def:method:EPSG::9807" /> <gml:usesParameterValue> <gml:value gml:uom="urn:ogc:def:uom:EPSG::9102">0</gml:value> <gml:valueOfParameter xlink:href="urn:ogc:def:parameter:EPSG::8801" /> </gml:usesParameterValue> <gml:usesParameterValue> <gml:value gml:uom="urn:ogc:def:uom:EPSG::9102">3</gml:value> <gml:valueOfParameter xlink:href="urn:ogc:def:parameter:EPSG::8802" /> </gml:usesParameterValue> <gml:usesParameterValue> <gml:value gml:uom="urn:ogc:def:uom:EPSG::9001">0.9996</gml:value> <gml:valueOfParameter xlink:href="urn:ogc:def:parameter:EPSG::8805" /> </gml:usesParameterValue> <gml:usesParameterValue> <gml:value gml:uom="urn:ogc:def:uom:EPSG::9001">500000</gml:value> <gml:valueOfParameter xlink:href="urn:ogc:def:parameter:EPSG::8806" /> </gml:usesParameterValue> <gml:usesParameterValue> <gml:value gml:uom="urn:ogc:def:uom:EPSG::9001">0</gml:value> <gml:valueOfParameter xlink:href="urn:ogc:def:parameter:EPSG::8807" /> </gml:usesParameterValue> </gml:Conversion> </gml:definedByConversion> <gml:usesCartesianCS> <gml:CartesianCS gml:id="ogrcrs10"> <gml:csName>Cartesian</gml:csName> <gml:csID> <gml:name gml:codeSpace="urn:ogc:def:cs:EPSG::">4400</gml:name> </gml:csID> <gml:usesAxis> <gml:CoordinateSystemAxis gml:id="ogrcrs11" gml:uom="urn:ogc:def:uom:EPSG::9001"> <gml:name>Easting</gml:name> <gml:axisID> <gml:name gml:codeSpace="urn:ogc:def:axis:EPSG::">9906</gml:name> </gml:axisID> <gml:axisAbbrev>E</gml:axisAbbrev> <gml:axisDirection>east</gml:axisDirection> </gml:CoordinateSystemAxis> </gml:usesAxis> <gml:usesAxis> <gml:CoordinateSystemAxis gml:id="ogrcrs12" gml:uom="urn:ogc:def:uom:EPSG::9001"> <gml:name>Northing</gml:name> <gml:axisID> <gml:name gml:codeSpace="urn:ogc:def:axis:EPSG::">9907</gml:name> </gml:axisID> <gml:axisAbbrev>N</gml:axisAbbrev> <gml:axisDirection>north</gml:axisDirection> </gml:CoordinateSystemAxis> </gml:usesAxis> </gml:CartesianCS> </gml:usesCartesianCS> </gml:ProjectedCRS> """ gdaltest.srs_wkt = """PROJCS["WGS 84 / UTM zone 31N",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.0174532925199433],AUTHORITY["EPSG","4326"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",3],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["Meter",1],AUTHORITY["EPSG","32631"]]""" srs = osr.SpatialReference() srs.ImportFromXML(gdaltest.srs_xml) got = srs.ExportToWkt() expected = gdaltest.srs_wkt if got != expected: print(got) return 'fail' return 'success'
def get_spatial_extent(raster_path, target_EPSG=4326, tol=0.1): """ Get the spatial extent of a raster file. If the file is not georeferenced (e.g. for raw radarsat 2), this function attempts to use the GCPs in the image . Howver, this doesn't always produce exact results, so it is advisable to use an extra buffer tolerance in your spatial extent (maybe ~0.1 decimal degrees) Parameters ---------- raster_path : str Path to raster for which a spatial extent is desired target_EPSG : int EPSG code specifying coordinate system for output file tol : float By how many decimal degrees to buffer spatial extent Returns ------- dict Dictionary with the following keys: {xmin, xmax, ymin, ymax} corresponding to the spatial extent in WGS84 decimal degrees """ # open file src = gdal.Open(raster_path) prj = src.GetProjection() # check if georeferencing information is available if (sum(src.GetGeoTransform()) == 2) and src.GetGCPCount() > 3: gt = gdal.GCPsToGeoTransform(src.GetGCPs()) prj = src.GetGCPProjection() else: gt = src.GetGeoTransform() # get untransformed upper left and lower right corner coordinates ulx, xres, xskew, uly, yskew, yres = gt lrx = ulx + (src.RasterXSize * xres) lry = uly + (src.RasterYSize * yres) # Setup the source projection - you can also import from epsg, proj4... source = osr.SpatialReference() source.ImportFromWkt(prj) # The target projection target = osr.SpatialReference() target.ImportFromEPSG(target_EPSG) # set up transform transform = osr.CoordinateTransformation(source, target) # transform coordinates upper = transform.TransformPoint(ulx, uly) lower = transform.TransformPoint(lrx, lry) ext = { 'xmin': min(upper[0], lower[0]) - tol, 'xmax': max(upper[0], lower[0]) + tol, 'ymin': min(upper[1], lower[1]) - tol, 'ymax': max(upper[1], lower[1]) + tol } return (ext)