max = featCount * len(inRasters) else: max = featCount * bands pb = progressBar(max + 1, 65) i = 0 start = time.time() # process points and rasters for f in fileInfos: i += 1 pb.update(i) gt = f.geotransform rasterCRS = f.projection #print "Layer", layerCRS.ExportToWkt() #print "Raster", rasterCRS.ExportToWkt() if needTransform: coordTransform = osr.CoordinateTransformation(layerCRS, rasterCRS) if coordTransform is None and needTransform: print 'Error while creating coordinate transformation.' sys.exit(1) if not gdalalloc: ds = gdal.Open(f.fileName) if f.bands == 1: shortName = f.fileBaseName[:10] if not gdalalloc: band = ds.ReadAsArray() inLayer.ResetReading() inFeat = inLayer.GetNextFeature() while inFeat is not None: i += 1 pb.update(i) geom = inFeat.GetGeometryRef()
def as_dataframe(self, **kwargs): """ Creates a dataframe object for a shapefile's main layer using layer_as_dataframe. This object is cached on disk for layer use, but the cached copy will only be picked up if the shapefile's mtime is older than the dataframe's mtime. :param shp: The shapefile :return: """ dfx_path = self.get_filename('dfx') if os.path.exists(dfx_path): try: self._df = pandas.read_pickle(dfx_path) except: os.unlink(self.get_filename('dfx')) if len(kwargs) != 0: cfg = self.resource.driver_config lyr = {} crx = xrc = None if 'bbox' in kwargs: minx, miny, maxx, maxy = kwargs['bbox'] if 'srs' in kwargs: if isinstance(kwargs['srs'], basestring): s_srs = osr.SpatialReference() if kwargs['srs'].lower().startswith('epsg:'): s_srs.ImportFromEPSG( int(kwargs['srs'].split(':')[1])) else: s_srs.ImportFromProj4(kwargs['srs']) else: s_srs = kwargs['srs'] t_srs = self.resource.srs if s_srs.ExportToProj4() != t_srs.ExportToProj4(): crx = osr.CoordinateTransformation(s_srs, t_srs) minx, miny, _ = crx.TransformPoint(minx, miny) maxx, maxy, _ = crx.TransformPoint(maxx, maxy) xrc = osr.CoordinateTransformation(t_srs, s_srs) lyr['bbox'] = (minx, miny, maxx, maxy) elif 'boundary' in kwargs: lyr['boundary'] = kwargs['boundary'] if 'query' in kwargs: lyr['query'] = [] if isinstance(kwargs['query'], basestring): query = json.loads(kwargs['query']) else: query = kwargs['query'] lyr['query'] = ' AND '.join( self.attrquery(key, value) for key, value in query.items()) start = kwargs['start'] if 'start' in kwargs else 0 count = start + kwargs['count'] if 'count' in kwargs else -1 # contsruct query cursor = self.connection # _cursor(**kwargs) q = "SELECT AsBinary({geometry_column}), * FROM ({table}) AS w WHERE " addand = False if 'bbox' in lyr: q += "ST_Intersects(GeomFromText('BBOX({xmin} {ymin} {xax} {ymax})'), {geometry_column})" addand = True if 'boundary' in lyr: q += "ST_Intersects(GeomFromText('{boundary}', {geometry_column}))" addand = True if 'query' in lyr: if addand: q += ' AND ' q += lyr['query'] if count > 0: q += ' LIMIT {count}'.format(count=count) table, geometry_column = self._table(**kwargs) if table.strip().lower().startswith('select'): table = '(' + table + ")" cursor.execute( q.format(boundary=cfg.get('boundary', None), geometry_column=geometry_column, table=table, **lyr.get('bbox', [None, None, None, None]))) for i in range(start): cursor.fetchone() names = [c[0] for c in cursor.description] throwaway_ix = names[1:].index(geometry_column) + 1 records = [] for record in cursor: records.append({ name: value for i, (name, value) in enumerate(zip(names, record)) if i != throwaway_ix }) records[-1]['geometry'] = wkb.loads(record['asbinary']) df = DataFrame.from_records(data=records, index='fid') if 'sort_by' in kwargs: df = df.sort_index(by=kwargs['sort_by']) return df elif hasattr(self, '_df'): return self._df else: table, geometry_column = self._table(**kwargs) query = "SELECT AsBinary({geometry_column}), * FROM {table}".format( table=table, geometry_column=geometry_column) cursor = self._cursor(**kwargs) cursor.execute("select load_extension('libspatialite.so')") cursor.execute(query) names = [c[0] for c in cursor.description] throwaway_ix = names[1:].index(geometry_column) + 1 records = [] for record in cursor: records.append({ name: value for i, (name, value) in enumerate(zip(names, record)) if i != throwaway_ix }) records[-1]['geometry'] = wkb.loads( str(records[-1]['AsBinary(GEOMETRY)'])) del records[-1]['AsBinary(GEOMETRY)'] df = DataFrame.from_records(data=records) df.to_pickle(dfx_path) self._df = df return self._df
def test_overlapping(shp_img, img): if shp_img.endswith('.shp'): # -------------- IN SHP ------------------- #print "Shapefile vs raster <br>" shp_ds = ogr.Open(shp_img) layer = shp_ds.GetLayer(0) INSR = layer.GetSpatialRef() INextent = layer.GetExtent() shp_ds = None if shp_img.endswith('.tif') or shp_img.endswith('.vrt'): # -------------- IN IMG ------------------- #print "Raster vs raster <br>" in_ds = gdal.Open(shp_img) INSR = osr.SpatialReference() INSR.ImportFromWkt(in_ds.GetProjectionRef()) geoTransform = in_ds.GetGeoTransform() minx = geoTransform[0] maxy = geoTransform[3] maxx = minx + geoTransform[1] * in_ds.RasterXSize miny = maxy + geoTransform[5] * in_ds.RasterYSize INextent = [minx, maxx, miny, maxy] in_ds = None print INextent # -------------- IN IMG ------------------- img_ds = gdal.Open(img) imgGeo = img_ds.GetGeoTransform() cols = img_ds.RasterXSize rows = img_ds.RasterYSize imgSR = osr.SpatialReference() imgSR.ImportFromWkt(img_ds.GetProjectionRef()) img_ds = None transform = osr.CoordinateTransformation(imgSR, INSR) (ulx, uly, holder) = transform.TransformPoint(imgGeo[0], imgGeo[3]) (lrx, lry, holder) = transform.TransformPoint(imgGeo[0] + cols * imgGeo[1], imgGeo[3] + rows * imgGeo[5]) wkt1 = "POLYGON ((" + str(INextent[0]) + " " + str( INextent[2]) + "," + str(INextent[1]) + " " + str( INextent[2]) + "," + str(INextent[1]) + " " + str( INextent[3]) + "," + str(INextent[0]) + " " + str( INextent[3]) + "," + str(INextent[0]) + " " + str( INextent[2]) + "))" wkt2 = "POLYGON ((" + str(ulx) + " " + str(uly) + "," + str( lrx) + " " + str(uly) + "," + str(lrx) + " " + str(lry) + "," + str( ulx) + " " + str(lry) + "," + str(ulx) + " " + str(uly) + "))" poly1 = ogr.CreateGeometryFromWkt(wkt1) poly2 = ogr.CreateGeometryFromWkt(wkt2) intersection = poly1.Intersection(poly2) #print intersection.ExportToWkt() return (intersection.ExportToWkt() != 'GEOMETRYCOLLECTION EMPTY')
# Load conservation attributes first. That's all we need from this ShapeFile conserve = os.path.join( top_level_folder, region, '01_Perennial_Network/03_Conservation_Restoration_Model/Conservation_Restoration_Model_Perennial_{}.shp' .format(region.replace('_', ''))) consatts = load_attributes(conserve, 'ReachID', ['oPBRC_UI', 'oPBRC_CR']) # Use the geometries from the capacity ShapeFile for the output capacity = os.path.join( top_level_folder, region, '01_Perennial_Network/02_Combined_Capacity_Model/Combined_Capacity_Model_Perennial_{}.shp' .format(region.replace('_', ''))) inDataSource = driver.Open(capacity, 0) inLayer = inDataSource.GetLayer() transform = osr.CoordinateTransformation(inLayer.GetSpatialRef(), outSpatialRef) for feature in inLayer: reachid = feature.GetField('ReachID') geom = feature.GetGeometryRef() geom.Transform(transform) # Create output Feature outFeature = ogr.Feature(outLayerDefn) outFeature.SetField('ReachID', reachid) outFeature.SetField('oCC_EX', feature.GetField('oCC_EX')) outFeature.SetField('oCC_HPE', feature.GetField('oCC_HPE')) outFeature.SetField('HUC', feature.GetField('HUC_ID')) outFeature.SetField('oPBRC_UI', risks[consatts[reachid]['oPBRC_UI']]) outFeature.SetField('oPBRC_CR', opportunities[consatts[reachid]['oPBRC_CR']])
def init_xy_with_subsets(self, lon_min, lat_min, lon_max, lat_max, target_cellsize_meters, subset_grid_shp, subset_grid_field_name=None): """Create & initialize x/y dimensions/coordinate vars and subset vars. Args: lon_min: Minimum longitude of domain. lat_min: Minimum latitude of domain. lon_max: Maximum longitude of domain. lat_max: Maximum latitude of domain. target_cellsize_meters: Target cell size, in meters. Actual calculated cell sizes will be approximations of this. subset_grid_shp: Path to subset grid polygon shapefile used to define subgrid domains. subset_grid_field_name: Optional, default None) Shapefile field name to be stored in the index file. Raises: Exception when given subset grid shapefile does not exist or does not include any grid polygons intersecting with given extent. Returns: Instance of `RegularGrid` representing the extended generated grid whose extent matches the union of all intersecting subset grid polygons. """ shp = ogr.Open(subset_grid_shp) layer = shp.GetLayer() # Create OGR Geometry from ocean model grid extent ring = ogr.Geometry(ogr.wkbLinearRing) ring.AddPoint(lon_min, lat_max) ring.AddPoint(lon_min, lat_min) ring.AddPoint(lon_max, lat_min) ring.AddPoint(lon_max, lat_max) ring.AddPoint(lon_min, lat_max) # Create polygon ofs_poly = ogr.Geometry(ogr.wkbPolygon) ofs_poly.AddGeometry(ring) # Get the EPSG value from the import shapefile and transform to WGS84 spatial_ref = layer.GetSpatialRef() shp_srs = spatial_ref.GetAttrValue('AUTHORITY', 1) source = osr.SpatialReference() source.ImportFromEPSG(int(shp_srs)) target = osr.SpatialReference() target.ImportFromEPSG(4326) transform = osr.CoordinateTransformation(source, target) ofs_poly.Transform(transform) # Find the intersection between grid polygon and ocean model grid extent subset_polys = {} fids = [] fields = {} fid = 0 for feature in layer: geom = feature.GetGeometryRef() if ofs_poly.Intersects(geom): subset_polys[fid] = geom.ExportToJson() if subset_grid_field_name is not None: field_name = feature.GetField(str(subset_grid_field_name)) fields.update({fid: field_name}) fids.append(fid) else: fids.append(fid) fid += 1 if len(fids) == 0: raise Exception('Given subset grid shapefile contains no polygons that intersect with model domain; cannot proceed.') # Use a single subset polygon to calculate x/y cell sizes. This ensures # that cells do not fall on the border between two grid polygons. single_polygon = ogr.Geometry(ogr.wkbMultiPolygon) single_polygon.AddGeometry(ogr.CreateGeometryFromJson(subset_polys[fids[0]])) sp_x_min, sp_x_max, sp_y_min, sp_y_max = single_polygon.GetEnvelope() cellsize_x, cellsize_y = RegularGrid.calc_cellsizes(sp_x_min, sp_y_min, sp_x_max, sp_y_max, target_cellsize_meters) # Combine identified subset grid polygons into single multipolygon to # calculate full extent of all combined subset grids multipolygon = ogr.Geometry(ogr.wkbMultiPolygon) for fid in fids: multipolygon.AddGeometry(ogr.CreateGeometryFromJson(subset_polys[fid])) (x_min, x_max, y_min, y_max) = multipolygon.GetEnvelope() full_reg_grid = RegularGrid(x_min, y_min, x_max, y_max, cellsize_x, cellsize_y) # Create NetCDF dimensions & coordinate variables using dimension sizes # from regular grid self.create_dims_coord_vars(len(full_reg_grid.y_coords), len(full_reg_grid.x_coords)) # Populate NetCDF coordinate variables using regular grid coordinates self.var_x[:] = full_reg_grid.x_coords[:] self.var_y[:] = full_reg_grid.y_coords[:] self.nc_file.gridSpacingLongitude = full_reg_grid.cellsize_x self.nc_file.gridSpacingLatitude = full_reg_grid.cellsize_y # Create subgrid dimension/variables self.create_subgrid_dims_vars(len(subset_polys), subset_grid_field_name) # Calculate subgrid mask ranges, populate subgrid ID for subgrid_index, fid in enumerate(fids): self.var_subgrid_id[subgrid_index] = fid if subset_grid_field_name is not None: self.var_subgrid_name[subgrid_index] = fields[fid] # Convert OGR geometry to shapely geometry subset_poly_shape = shape(json.loads(subset_polys[fid])) min_x_coord = subset_poly_shape.bounds[0] max_x_coord = subset_poly_shape.bounds[2] min_y_coord = subset_poly_shape.bounds[1] max_y_coord = subset_poly_shape.bounds[3] subgrid_x_min = None subgrid_x_max = None subgrid_y_min = None subgrid_y_max = None for i, x in enumerate(self.var_x): if x >= min_x_coord: subgrid_x_min = i break count_x = round(((max_x_coord - min_x_coord) / full_reg_grid.cellsize_x)) for i, y in enumerate(self.var_y): if y >= min_y_coord: subgrid_y_min = i break count_y = round(((max_y_coord - min_y_coord) / full_reg_grid.cellsize_y)) subgrid_x_max = subgrid_x_min + count_x - 1 subgrid_y_max = subgrid_y_min + count_y - 1 self.var_subgrid_x_min[subgrid_index] = subgrid_x_min self.var_subgrid_x_max[subgrid_index] = subgrid_x_max self.var_subgrid_y_min[subgrid_index] = subgrid_y_min self.var_subgrid_y_max[subgrid_index] = subgrid_y_max return full_reg_grid
tfb = tfz.open("Train Station Codes and Chinese Names.csv", "r") tf = codecs.getreader('utf-16')(tfb) sfName = "TrainStation_Oct2017/MRTLRTStnPtt" shp = esri_driver.Open('/vsizip/stations.zip/' + sfName + '.shp', 0) # read only if shp is None: print('Cannot open shapefile') exit(1) layer = shp.GetLayer(0) # Setup a transform to EPSG 4326 targetsr = osr.SpatialReference() targetsr.ImportFromEPSG(4326) transform = osr.CoordinateTransformation(layer.GetSpatialRef(), targetsr) # Read in the station names tf_reader = csv.DictReader(tf, delimiter="\t") names = {} for tf_record in tf_reader: names[tf_record['stn_code']] = (tf_record['mrt_station_english'], tf_record['mrt_station_chinese']) coordinates = {} for shapeRec in layer: geom = shapeRec.GetGeometryRef() geom.Transform(transform) for stn_id in map(lambda x: x.strip(),
def FlowlineToPoint(in_drainage_line, river_id, out_csv_file, file_geodatabase=None): """ Converts flowline feature to a list of centroid points with their rivid in EPSG:4326. Parameters ---------- in_drainage_line: str Path to the stream network (i.e. Drainage Line) shapefile. river_id: str The name of the field with the river ID (Ex. 'HydroID', 'COMID', or 'LINKNO'). out_csv_file: str Path to the output csv file with the centroid points. file_geodatabase: str, optional Path to the file geodatabase. If you use this option, in_drainage_line is the name of the stream network feature class (WARNING: Not always stable with GDAL). Example:: from RAPIDpy.gis.centroid import FlowlineToPoint FlowlineToPoint( in_drainage_line='/path/to/drainageline.shp', river_id='LINKNO', out_csv_file='/path/to/comid_lat_lon_z.csv') """ ogr_drainage_line_shapefile_lyr, ogr_drainage_line_shapefile = \ open_shapefile(in_drainage_line, file_geodatabase) ogr_drainage_line_shapefile_lyr_proj = \ ogr_drainage_line_shapefile_lyr.GetSpatialRef() osr_geographic_proj = osr.SpatialReference() osr_geographic_proj.ImportFromEPSG(4326) proj_transform = None if ogr_drainage_line_shapefile_lyr_proj != osr_geographic_proj: proj_transform = osr.CoordinateTransformation( ogr_drainage_line_shapefile_lyr_proj, osr_geographic_proj) # print valid field names to table with open_csv(out_csv_file, 'w') as outfile: writer = csv_writer(outfile) writer.writerow(['rivid', 'lat', 'lon', 'z']) for feature in ogr_drainage_line_shapefile_lyr: feat_geom = feature.GetGeometryRef() if proj_transform: feat_geom.Transform(proj_transform) centroid = feat_geom.Centroid() centroid_pt = centroid.GetPoint(0) writer.writerow([ feature.GetField(river_id), centroid_pt[1], centroid_pt[0], centroid_pt[2] ]) del ogr_drainage_line_shapefile
def ParameterGenerator(S2TileGranule, SrcEPSG, TargetEPSG, ReprojectedImgPath, ParOutPath): st_time = time.time() src_srs = osr.SpatialReference() src_srs.ImportFromEPSG(SrcEPSG) target_srs = osr.SpatialReference() target_srs.ImportFromEPSG(TargetEPSG) ct = osr.CoordinateTransformation(src_srs, target_srs) ct_inv = osr.CoordinateTransformation(target_srs, src_srs) g = gdal.Open(S2TileGranule) geo_t = g.GetGeoTransform() x_size = g.RasterXSize # Raster xsize y_size = g.RasterYSize # Raster ysize pixel_spacing = geo_t[1] # 50度带的参数 p1x, p1y = geo_t[0], geo_t[3] p2x, p2y = geo_t[0] + pixel_spacing * x_size, geo_t[3] p3x, p3y = geo_t[0] + pixel_spacing * x_size, geo_t[ 3] - pixel_spacing * y_size p4x, p4y = geo_t[0], geo_t[3] - pixel_spacing * y_size end_time1 = time.time() print("1", end_time1 - st_time) # 投影后顶点坐标 rep1x, rep1y = ct.TransformPoint(p1x, p1y)[0:2] rep2x, rep2y = ct.TransformPoint(p2x, p2y)[0:2] rep3x, rep3y = ct.TransformPoint(p3x, p3y)[0:2] rep4x, rep4y = ct.TransformPoint(p4x, p4y)[0:2] minX = min(rep1x, rep4x) maxX = max(rep2x, rep3x) minY = min(rep3y, rep4y) maxY = max(rep1y, rep2y) # 49度带的对角坐标 ulx = int(minX / pixel_spacing) * pixel_spacing uly = int(np.ceil((maxY) / pixel_spacing)) * pixel_spacing lrx = int(np.ceil((maxX) / pixel_spacing)) * pixel_spacing lry = int(minY / pixel_spacing) * pixel_spacing print('Geotransform', [ulx, pixel_spacing, 0, uly, 0, -pixel_spacing]) end_time2 = time.time() print("2", end_time2 - end_time1) # 49度带的宽高 Width = int((lrx - ulx) / pixel_spacing) Heigth = int((uly - lry) / pixel_spacing) print("W H", Width, Heigth) print(ulx, uly, Width, Heigth) WD1 = ulx + np.repeat(a=[np.arange(Width)], repeats=Heigth, axis=0 ).flatten() * pixel_spacing + 0.5 * pixel_spacing HD1 = uly - np.repeat(a=np.arange(Heigth), repeats=Width) * pixel_spacing - 0.5 * pixel_spacing Pair = np.column_stack((WD1, HD1)) end_time3 = time.time() print("3", end_time3 - end_time2) # 49度带转50度 Pair = ct_inv.TransformPoints(Pair) end_time4 = time.time() print("4", end_time4 - end_time3) # print(np.array(Pair).reshape(1926,1927,3)) PairX = np.array([p[0] for p in Pair]).reshape(Heigth, Width) PairY = np.array([p[1] for p in Pair]).reshape(Heigth, Width) # print(PairX) mask = (PairX < p1x) | (PairX >= p2x) | (PairY > p1y) | (PairY <= p4y) PairX[mask] = 0 PairY[mask] = 0 Valid = PairX != 0 COL = np.floor((PairX - p1x) / pixel_spacing).astype(np.int32) ROW = np.floor((p1y - PairY) / pixel_spacing).astype(np.int32) ECord = WD1.reshape(Heigth, Width) NCord = HD1.reshape(Heigth, Width) end_time5 = time.time() print("5", end_time5 - end_time4) ParaMeterX = np.zeros(shape=(y_size, x_size), dtype=np.float) ParaMeterY = np.zeros(shape=(y_size, x_size), dtype=np.float) ParaMeterXGapFiller = np.zeros(shape=(y_size, x_size), dtype=np.float) ParaMeterYGapFiller = np.zeros(shape=(y_size, x_size), dtype=np.float) ValidCOL = COL[Valid] ValidROW = ROW[Valid] ValidRC = ValidCOL + ValidROW * x_size np.put(a=ParaMeterX, ind=ValidRC, v=ECord[Valid]) np.put(a=ParaMeterY, ind=ValidRC, v=NCord[Valid]) print(ParaMeterY.min()) end_time6 = time.time() print("6", end_time6 - end_time5) ReprojectedImg = gdal.Open(ReprojectedImgPath).ReadAsArray() ReprojectedImg[mask] = -2 # from matplotlib import pyplot as plt # plt.imshow(ReprojectedImg) # plt.show() Empty = np.where(ReprojectedImg == 0) EpValidCOL = COL[Empty] EpValidROW = ROW[Empty] EpValidRC = EpValidCOL + EpValidROW * x_size np.put(a=ParaMeterXGapFiller, ind=EpValidRC, v=ECord[Empty]) np.put(a=ParaMeterYGapFiller, ind=EpValidRC, v=NCord[Empty]) Xpath = os.path.join(ParOutPath, 'TestRepX{}GapFiller.tif'.format(pixel_spacing)) Ypath = os.path.join(ParOutPath, 'TestRepY{}GapFiller.tif'.format(pixel_spacing)) write_Img(ParaMeterXGapFiller, Xpath, src_srs, geo_t, x_size, y_size, im_bands=1, dtype=gdal.GDT_UInt32) write_Img(ParaMeterYGapFiller, Ypath, src_srs, geo_t, x_size, y_size, im_bands=1, dtype=gdal.GDT_UInt32) end_time7 = time.time() print("7", end_time7 - end_time6) end_time = time.time() print("I am done ! take time for {} s".format(end_time - st_time))
def main(): # pixel sizes between images are considered equal if diff is less than pixel_eps (UTM) pixel_eps = 0.01 i = readInputs() print('PreImage: ' + i.PreImage) print('PostImage: ' + i.PostImage) prefile = i.PreImage postfile = i.PostImage # geographic coords. pre_multipolygon, gcsSpatialRef = geotiff2boundary(prefile) pre_raster = gdal.Open(prefile) pre_outSpatialRef = osr.SpatialReference() pre_outSpatialRef.ImportFromWkt(pre_raster.GetProjectionRef()) coordTransPre = osr.CoordinateTransformation(gcsSpatialRef, pre_outSpatialRef) pre_gt = pre_raster.GetGeoTransform() post_multipolygon, gcsSpatialRef = geotiff2boundary(postfile) post_raster = gdal.Open(postfile) post_outSpatialRef = osr.SpatialReference() post_outSpatialRef.ImportFromWkt(post_raster.GetProjectionRef()) coordTransPost = osr.CoordinateTransformation(gcsSpatialRef, post_outSpatialRef) post_gt = post_raster.GetGeoTransform() # determine pixel width if abs(pre_gt[1] - post_gt[1]) < pixel_eps: pixelWidth = abs(pre_gt[1]) else: print('Error: Pixel widths differ between images ' + prefile + ' and ' + postfile) sys.exit(1) # determine pixel height if abs(pre_gt[5] - post_gt[5]) < pixel_eps: pixelHeight = abs(pre_gt[5]) else: print('Error: Pixel heights differ between images ' + prefile + ' and ' + postfile) sys.exit(1) # test geotiff2boundary(inGeotiff) # re-project post_multipolygon if needed if pre_outSpatialRef == post_outSpatialRef: pre_multipolygon.Transform(coordTransPre) post_multipolygon.Transform(coordTransPost) else: print('Needed to re-project post_multipolygon.') pre_multipolygon.Transform(coordTransPre) post_multipolygon.Transform(coordTransPre) # shapefile_generation(pre_outSpatialRef,pre_multipolygon,'pre_polygon.shp') # coordTrans = osr.CoordinateTransformation(vectorSpatialRef, rasterSpatialRef) # polygon.Transform(coordTrans) # shapefile_generation(post_outSpatialRef,post_multipolygon,'post_polygon.shp') # print 'pre_multipolygon:' # print pre_multipolygon # print ' ' # print 'pre_outSpatialRef:' # print pre_outSpatialRef # print ' ' # print 'post_multipolygon:' # print post_multipolygon # print ' ' # print 'post_outSpatialRef:' # print post_outSpatialRef # print ' ' if i.aoi is None: # if an area-of-interest is NOT defined overlap_poly = overlap_polygons(pre_multipolygon, post_multipolygon) overlap_SpatialRef = pre_outSpatialRef else: # if an area-of-interest is defined overlap_poly_temp = overlap_polygons(pre_multipolygon, post_multipolygon) overlap_SpatialRef = pre_outSpatialRef AOI_poly, AOI_SpatialRef = shape2geometry_only(i.aoi) AOI_poly.Transform(coordTransPre) if ogr.Geometry.Contains(overlap_poly_temp, AOI_poly): # if area-of-interest is fully contained by the intersecton of granule scenes overlap_poly = AOI_poly elif ogr.Geometry.Overlaps(overlap_poly_temp, AOI_poly): # if only part of the area-of-interest lies within the intersection of granule scenes overlap_poly = overlap_polygons(overlap_poly_temp, AOI_poly) else: # if the area-of-interest is completely disjointed from the intersection of the granule scenes print( 'Error: Area of interest is disjointed from the intersection of the granule scenes.' ) print( ' The intersection of scenes will be used for analysis instead of the area of interest.' ) overlap_poly = overlap_poly_temp # overlap_poly = pre_multipolygon.Intersection(post_multipolygon) # print overlap_poly # print overlap_poly.GetEnvelope() # print dir(overlap_poly) # print overlap_poly.GetGeometryName() # if overlap_poly.GetGeometryName()=='MULTIPOLYGON': # for polygon in overlap_poly: # print polygon.GetEnvelope() # print dir(polygon) # print overlap_poly # print overlap_poly.GetPoints() # print overlap_poly.GetExtent() env = overlap_poly.GetEnvelope() # print 'xmin='+str(env[0]) # print 'xmax='+str(env[1]) # print 'ymin='+str(env[2]) # print 'ymax='+str(env[3]) # print 'xRange='+str(env[1]-env[0]) # print 'yRange='+str(env[3]-env[2]) xMin = env[0] xMax = env[1] yMin = env[2] yMax = env[3] xRange = xMax - xMin yRange = yMax - yMin colRange = np.ceil((xRange + 1) / pixelWidth) rowRange = np.ceil((yRange + 1) / pixelHeight) # print 'pre width and height = '+str(pre_gt[1])+' '+str(pre_gt[5]) # print 'post width and height = '+str(post_gt[1])+' '+str(post_gt[5]) # print 'number of columns = '+str(colRange) # print 'number of rows = '+str(rowRange) # generate overlap.shp shapefile_generation(overlap_SpatialRef, overlap_poly, 'overlap.shp') if i.bb: # generate overlap_BB.shp (shapefile of bounding box of overlap.shp) ring_BB = ogr.Geometry(ogr.wkbLinearRing) ring_BB.AddPoint_2D(xMin, yMin) ring_BB.AddPoint_2D(xMin, yMax) ring_BB.AddPoint_2D(xMax, yMax) ring_BB.AddPoint_2D(xMax, yMin) ring_BB.AddPoint_2D(xMin, yMin) overlap_BB_poly = ogr.Geometry(ogr.wkbPolygon) overlap_BB_poly.AddGeometry(ring_BB) shapefile_generation(overlap_SpatialRef, overlap_BB_poly, 'overlap_BB.shp') CropShapeFile = 'overlap_BB.shp' else: CropShapeFile = 'overlap.shp' # cmd1=' '.join(['gdalwarp','-cutline overlap.shp','-crop_to_cutline', \ # '-overwrite','-dstnodata 0',prefile,'PreImage_Crop_geo.tif']) # cmd2=' '.join(['gdalwarp','-cutline overlap.shp','-crop_to_cutline', \ # '-overwrite','-dstnodata 0',postfile,'PostImage_Crop_geo.tif']) # os.system(cmd1) # os.system(cmd2) if colRange <= i.c and rowRange <= i.r: # if the bounding box of the overlap is small enough, then crop it and use it print('Cropping images...') gdal.Warp('PreImage_Crop_geo.tif', prefile, cutlineDSName=CropShapeFile, cropToCutline=True, dstNodata=0) gdal.Warp('PostImage_Crop_geo.tif', postfile, cutlineDSName=CropShapeFile, cropToCutline=True, dstNodata=0) else: # if the overlap is too large, decimate/resample the image xFactor = colRange / i.c yFactor = rowRange / i.r Factor = max(np.ceil(xFactor), np.ceil(yFactor)) print('Cropping images...') gdal.Warp('pre_temp_geo.tif', prefile, cutlineDSName=CropShapeFile, cropToCutline=True, dstNodata=0) gdal.Warp('post_temp_geo.tif', postfile, cutlineDSName=CropShapeFile, cropToCutline=True, dstNodata=0) # down-sample and save final geotiffs gdal.Translate('PreImage_Crop_geo.tif', 'pre_temp_geo.tif', xRes=pixelWidth * Factor, yRes=pixelHeight * Factor, resampleAlg='average', noData=0) gdal.Translate('PostImage_Crop_geo.tif', 'post_temp_geo.tif', xRes=pixelWidth * Factor, yRes=pixelHeight * Factor, resampleAlg='average', noData=0) print('Needed to decimate/resample images by a factor of ' + str(Factor) + '...') # delete temporary geotiffs os.remove('pre_temp_geo.tif') os.remove('post_temp_geo.tif')
def reproject_vector( path, epsg_from=None, epsg_to=None): """ reproject a vector file (only the first layer!) (it does not save the dataset to disk) :param path: the path to the input vectorfile :param epsg_from: the input spatial reference; in None take the source reference :param epsg_to: the output spatial reference :return: the reprojected dataset """ if not epsg_to: raise Exception("please, specify the output EPSG codes") inDataSet = None outDataSet = None inFeature = None outFeature = None outLayer = None try: driver = ogr.GetDriverByName('ESRI Shapefile') inDataSet = driver.Open(path, 0) # 0 means read-only # define input SpatialReference if not epsg_from: layer = inDataSet.GetLayer() inSpatialRef = layer.GetSpatialRef() else: inSpatialRef = osr.SpatialReference() inSpatialRef.ImportFromEPSG(epsg_from) # define output SpatialReference outSpatialRef = osr.SpatialReference() outSpatialRef.ImportFromEPSG(epsg_to) # create the CoordinateTransformation coordTrans = osr.CoordinateTransformation(inSpatialRef, outSpatialRef) # get the first input layer and the geometry type inLayer = inDataSet.GetLayer() geotype = inLayer.GetGeomType() lname = inLayer.GetName() drv = ogr.GetDriverByName("ESRI Shapefile") outDataSet = drv.CreateDataSource("/vsimem/memory.shp") outLayer = outDataSet.CreateLayer(lname, srs=outSpatialRef, geom_type=geotype) # add fields inLayerDefn = inLayer.GetLayerDefn() for i in range(0, inLayerDefn.GetFieldCount()): fieldDefn = inLayerDefn.GetFieldDefn(i) outLayer.CreateField(fieldDefn) # get the output layer"s feature definition outLayerDefn = outLayer.GetLayerDefn() counter = 1 # loop through the input features inFeature = inLayer.GetNextFeature() while inFeature: # get the input geometry geom = inFeature.GetGeometryRef() # reproject the geometry geom.Transform(coordTrans) # create a new feature outFeature = ogr.Feature(outLayerDefn) # set the geometry and attribute outFeature.SetGeometry(geom) for i in range(0, outLayerDefn.GetFieldCount()): outFeature.SetField(outLayerDefn.GetFieldDefn(i).GetNameRef(), inFeature.GetField(i)) # add the feature to the shapefile outLayer.CreateFeature(outFeature) # destroy the features and get the next input feature if outFeature: outFeature = None inFeature = inLayer.GetNextFeature() counter += 1 #print(counter) return outDataSet except RuntimeError as err: raise err except Exception as e: raise e finally: if inDataSet: outDataSet == None # give back control to C++ if outDataSet: outDataSet == None if outLayer: outLayer == None if inFeature: inFeature == None if outFeature: outFeature = None
# Open the image to get the geo data src = gdal.Open(image) # Load the image with opencv img = cv2.imread(image) # The source projection (wgs84, coordinate system used by gps) source = osr.SpatialReference() source.ImportFromWkt(__wgs84_wkt) # The target projection (whatever is used by the geotiff image) target = osr.SpatialReference() target.ImportFromWkt(src.GetProjection()) # Create the transform and an inverse transform __transform = osr.CoordinateTransformation(source, target) __transform_inv = osr.CoordinateTransformation(target, source) # Get the coordinates of the upper left and lower right point of the image # (uses whatever coordinate system the image was encoded with) ulx, xres, xskew, uly, yskew, yres = src.GetGeoTransform() lrx = ulx + (src.RasterXSize * xres) lry = uly + (src.RasterYSize * yres) # Get the height and width of the image (in pixels) (hei, wid, cha) = img.shape # Determine values to interpolate between the geo coordinate system of # the image and pixel values xScale = wid / (lrx - ulx) yScale = hei / (lry - uly)
elif '_hnv' in plt_src.lower(): sheet_type = 'HNV' if not os.path.isdir(tgt_dir): os.makedirs(tgt_dir) src_sr = osr.SpatialReference() src_sr.SetWellKnownGeogCS('WGS84') src_sr.SetUTM(utm_zone) tgt_sr = osr.SpatialReference() tgt_sr.SetWellKnownGeogCS('WGS84') gm_tpl = "http://maps.googleapis.com/maps/api/staticmap?center=[x],[y]&zoom=13&size=310x150&sensor=false&markers=color:green|[x],[y]" ct = osr.CoordinateTransformation(src_sr, tgt_sr) csv_reader = csv.DictReader(open(plt_src, 'rb'), delimiter=";") species = sorted( csv_reader.fieldnames[csv_reader.fieldnames.index('Humidity') + 1:]) record_count = 0 for row in csv_reader: # retrieving plot id and x and y coordinates plot = int(row['Plot']) x = int(row["X_UTM%d" % utm_zone]) y = int(row["Y_UTM%d" % utm_zone]) # incrementing record count record_count += 1
def mod3dtokmz(self): """ Saves the 3D model and grids in a kmz file. Note: Only the boundary of the area is in degrees. The actual coordinates are still in meters. """ mvis_3d = mvis3d.Mod3dDisplay() mvis_3d.lmod1 = self.lmod rev = 1 # should be 1 normally xrng = np.array(self.lmod.xrange, dtype=float) yrng = np.array(self.lmod.yrange, dtype=float) zrng = np.array(self.lmod.zrange, dtype=float) if 'Raster' in self.indata: wkt = self.indata['Raster'][0].wkt else: wkt = '' prjkmz = Exportkmz(wkt) tmp = prjkmz.exec_() if tmp == 0: return if prjkmz.proj.wkt == '': QtWidgets.QMessageBox.warning(self.parent, 'Warning', ' You need a projection!', QtWidgets.QMessageBox.Ok) return smooth = prjkmz.checkbox_smooth.isChecked() orig_wkt = prjkmz.proj.wkt orig = osr.SpatialReference() orig.ImportFromWkt(orig_wkt) targ = osr.SpatialReference() targ.SetWellKnownGeogCS('WGS84') prj = osr.CoordinateTransformation(orig, targ) res = prj.TransformPoint(xrng[0], yrng[0]) lonwest, latsouth = res[0], res[1] res = prj.TransformPoint(xrng[1], yrng[1]) loneast, latnorth = res[0], res[1] # Get Save Name filename = self.ifile self.showtext('kmz export starting...') # Move to 3d model tab to update the model stuff self.showtext('updating 3d model...') mvis_3d.spacing = [self.lmod.dxy, self.lmod.dxy, self.lmod.d_z] mvis_3d.origin = [xrng[0], yrng[0], zrng[0]] mvis_3d.gdata = self.lmod.lith_index[::1, ::1, ::-1] itmp = np.sort(np.unique(self.lmod.lith_index)) itmp = itmp[itmp > 0] tmp = np.ones((255, 4)) * 255 for i in itmp: tmp[i, :3] = self.lmod.mlut[i] mvis_3d.lut = tmp mvis_3d.update_model(smooth) self.showtext('creating kmz file') heading = str(0.) tilt = str(45.) # angle from vertical lat = str(np.mean([latsouth, latnorth])) # coord of object lon = str(np.mean([lonwest, loneast])) # coord of object rng = str(max(xrng.ptp(), yrng.ptp(), zrng.ptp())) # range to object alt = str(0) # alt of object eye is looking at (meters) lato = str(latsouth) lono = str(lonwest) # update colors self.lmod.update_lith_list_reverse() dockml = ('<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\r\n' '<kml xmlns="http://www.opengis.net/kml/2.2" ' 'xmlns:gx="http://www.google.com/kml/ext/2.2">\r\n' '\r\n' ' <Folder>\r\n' ' <name>Lithological Model</name>\r\n' ' <description>Created with PyGMI</description>\r\n' ' <visibility>1</visibility>\r\n' ' <LookAt>\r\n' ' <heading>' + heading + '</heading>\r\n' ' <tilt>' + tilt + '</tilt>\r\n' ' <latitude>' + lat + '</latitude>\r\n' ' <longitude>' + lon + '</longitude>\r\n' ' <range>' + rng + '</range>\r\n' ' <altitude>' + alt + '</altitude>\r\n' ' </LookAt>\r\n') mvis_3d.update_for_kmz() modeldae = [] lkey = list(mvis_3d.faces.keys()) lkey.pop(lkey.index(0)) lithcnt = -1 alt = str(0) for lith in lkey: faces = np.array(mvis_3d.gfaces[lith]) # Google wants the model to have origin (0,0) points = mvis_3d.gpoints[lith] if points == []: continue points -= mvis_3d.origin x = points[:, 0] y = points[:, 1] earthrad = 6378137. z = earthrad - np.sqrt(earthrad**2 - (x**2 + y**2)) points[:, 2] -= z if rev == -1: points += [xrng.ptp(), yrng.ptp(), 0] norm = np.abs(mvis_3d.gnorms[lith]) clrtmp = np.array(self.lmod.mlut[lith]) / 255. curmod = self.lmod.lith_list_reverse[lith] if len(points) > 60000: self.showtext(curmod + ' has too many points (' + str(len(points)) + '). Not exported') points = points[:60000] norm = norm[:60000] faces = faces[faces.max(1) < 60000] lithcnt += 1 dockml += (' <Placemark>\r\n' ' <name>' + curmod + '</name>\r\n' ' <description></description>\r\n' ' <Style id="default"/>\r\n' ' <Model>\r\n' ' <altitudeMode>absolute</altitudeMode>\r\n' ' <Location>\r\n' ' <latitude>' + lato + '</latitude>\r\n' ' <longitude>' + lono + '</longitude>\r\n' ' <altitude>' + str(alt) + '</altitude>\r\n' ' </Location>\r\n' ' <Orientation>\r\n' ' <heading>0</heading>\r\n' ' <tilt>0</tilt>\r\n' ' <roll>0</roll>\r\n' ' </Orientation>\r\n' ' <Scale>\r\n' ' <x>1</x>\r\n' ' <y>1</y>\r\n' ' <z>1</z>\r\n' ' </Scale>\r\n' ' <Link>\r\n' ' <href>models/mod3d' + str(lithcnt) + '.dae</href>\r\n' ' </Link>\r\n' ' </Model>\r\n' ' </Placemark>\r\n') position = str(points.flatten().tolist()) position = position.replace('[', '') position = position.replace(']', '') position = position.replace(',', '') vertex = str(faces.flatten().tolist()) vertex = vertex.replace('[', '') vertex = vertex.replace(']', '') vertex = vertex.replace(',', '') normal = str(norm.flatten().tolist()) normal = normal.replace('[', '') normal = normal.replace(']', '') normal = normal.replace(',', '') color = str(clrtmp.flatten().tolist()) color = color.replace('[', '') color = color.replace(']', '') color = color.replace(',', '') modeldae.append( '<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\r\n' '<COLLADA xmlns="http://www.collada.org/2005' '/11/COLLADASchema" ' 'version="1.4.1">\r\n' ' <asset>\r\n' ' <contributor>\r\n' ' <authoring_tool>PyGMI</authoring_tool>\r\n' ' </contributor>\r\n' ' <created>2012-03-01T10:36:38Z</created>\r\n' ' <modified>2012-03-01T10:36:38Z</modified>\r\n' ' <up_axis>Z_UP</up_axis>\r\n' ' </asset>\r\n' ' <library_visual_scenes>\r\n' ' <visual_scene id="ID1">\r\n' ' <node name="SketchUp">\r\n' ' <node id="ID2" name="instance_0">\r\n' ' <matrix> 1 0 0 0 \r\n' ' 0 1 0 0 \r\n' ' 0 0 1 0 \r\n' ' 0 0 0 1 \r\n' ' </matrix>\r\n' ' <instance_node url="#ID3" />\r\n' ' </node>\r\n' ' </node>\r\n' ' </visual_scene>\r\n' ' </library_visual_scenes>\r\n' ' <library_nodes>\r\n' ' <node id="ID3" name="skp489E">\r\n' ' <instance_geometry url="#ID4">\r\n' ' <bind_material>\r\n' ' <technique_common>\r\n' ' <instance_material symbol="Material2"' ' target="#ID5">\r\n' ' <bind_vertex_input semantic="UVSET0" ' 'input_semantic="TEXCOORD" input_set="0" />\r\n' ' </instance_material>\r\n' ' </technique_common>\r\n' ' </bind_material>\r\n' ' </instance_geometry>\r\n' ' </node>\r\n' ' </library_nodes>\r\n' ' <library_geometries>\r\n' ' <geometry id="ID4">\r\n' ' <mesh>\r\n' ' <source id="ID7">\r\n' ' <float_array id="ID10" count="' + str(points.size) + '">' + position + ' </float_array>\r\n' ' <technique_common>\r\n' ' <accessor count="' + str(points.shape[0]) + '" source="#ID10" stride="3">\r\n' ' <param name="X" type="float" />\r\n' ' <param name="Y" type="float" />\r\n' ' <param name="Z" type="float" />\r\n' ' </accessor>\r\n' ' </technique_common>\r\n' ' </source>\r\n' ' <source id="ID8">\r\n' ' <float_array id="ID11" count="' + str(norm.size) + '">' + normal + ' </float_array>\r\n' ' <technique_common>\r\n' ' <accessor count="' + str(norm.shape[0]) + '" source="#ID11" stride="3">\r\n' ' <param name="X" type="float" />\r\n' ' <param name="Y" type="float" />\r\n' ' <param name="Z" type="float" />\r\n' ' </accessor>\r\n' ' </technique_common>\r\n' ' </source>\r\n' ' <vertices id="ID9">\r\n' ' <input semantic="POSITION" source="#ID7" />\r\n' ' <input semantic="NORMAL" source="#ID8" />\r\n' ' </vertices>\r\n' ' <triangles count="' + str(faces.shape[0]) + '" material="Material2">\r\n' ' <input offset="0" semantic="VERTEX" ' 'source="#ID9" />\r\n' ' <p>' + vertex + '</p>\r\n' ' </triangles>\r\n' ' </mesh>\r\n' ' </geometry>\r\n' ' </library_geometries>\r\n' ' <library_materials>\r\n' ' <material id="ID5" name="__auto_">\r\n' ' <instance_effect url="#ID6" />\r\n' ' </material>\r\n' ' </library_materials>\r\n' ' <library_effects>\r\n' ' <effect id="ID6">\r\n' ' <profile_COMMON>\r\n' ' <technique sid="COMMON">\r\n' ' <lambert>\r\n' ' <diffuse>\r\n' ' <color>' + color + '</color>\r\n' ' </diffuse>\r\n' ' </lambert>\r\n' ' </technique>\r\n' ' <extra> />\r\n' ' <technique profile="GOOGLEEARTH"> />\r\n' ' <double_sided>1</double_sided> />\r\n' ' </technique> />\r\n' ' </extra> />\r\n' ' </profile_COMMON>\r\n' ' </effect>\r\n' ' </library_effects>\r\n' ' <scene>\r\n' ' <instance_visual_scene url="#ID1" />\r\n' ' </scene>\r\n' '</COLLADA>') zfile = zipfile.ZipFile(filename, 'w') for i, _ in enumerate(modeldae): zfile.writestr('models\\mod3d' + str(i) + '.dae', modeldae[i]) for i in self.lmod.griddata: x_1, x_2, y_1, y_2 = self.lmod.griddata[i].extent res = prj.TransformPoint(x_1, y_1) lonwest, latsouth = res[0], res[1] res = prj.TransformPoint(x_2, y_2) loneast, latnorth = res[0], res[1] dockml += (' <GroundOverlay>\r\n' ' <name>' + i + '</name>\r\n' ' <description></description>\r\n' ' <Icon>\r\n' ' <href>models/' + i + '.png</href>\r\n' ' </Icon>\r\n' ' <LatLonBox>\r\n' ' <north>' + str(latnorth) + '</north>\r\n' ' <south>' + str(latsouth) + '</south>\r\n' ' <east>' + str(loneast) + '</east>\r\n' ' <west>' + str(lonwest) + '</west>\r\n' ' <rotation>0.0</rotation>\r\n' ' </LatLonBox>\r\n' ' </GroundOverlay>\r\n') fig = plt.figure('tmp930', frameon=False) ax1 = plt.Axes(fig, [0., 0., 1., 1.]) ax1.set_axis_off() fig.add_axes(ax1) plt.imshow(self.lmod.griddata[i].data, extent=(lonwest, loneast, latsouth, latnorth), aspect='auto', interpolation='nearest') plt.savefig('tmp930.png') zfile.write('tmp930.png', 'models\\' + i + '.png') os.remove('tmp930.png') dockml += (' </Folder>\r\n' ' \r\n' ' </kml>') zfile.writestr('doc.kml', dockml) zfile.close() self.showtext('kmz export complete!')
def main(): from dateutil.parser import parse try: from pygbif import occurrences from pygbif import species except ImportError: grass.fatal( _("Cannot import pygbif (https://github.com/sckott/pygbif)" " library." " Please install it (pip install pygbif)" " or ensure that it is on path" " (use PYTHONPATH variable).")) # Parse input options output = options["output"] mask = options["mask"] species_maps = flags["i"] no_region_limit = flags["r"] no_topo = flags["b"] print_species = flags["p"] print_species_table = flags["t"] print_species_shell = flags["g"] print_occ_number = flags["o"] allow_no_geom = flags["n"] hasGeoIssue = flags["s"] taxa_list = options["taxa"].split(",") institutionCode = options["institutioncode"] basisofrecord = options["basisofrecord"] recordedby = options["recordedby"].split(",") date_from = options["date_from"] date_to = options["date_to"] country = options["country"] continent = options["continent"] rank = options["rank"] # Define static variable # Initialize cat cat = 0 # Number of occurrences to fetch in one request chunk_size = 300 # lat/lon proj string latlon_crs = [ "+proj=longlat +no_defs +a=6378137 +rf=298.257223563 +towgs84=0.000,0.000,0.000", "+proj=longlat +no_defs +a=6378137 +rf=298.257223563 +towgs84=0,0,0,0,0,0,0", "+proj=longlat +no_defs +a=6378137 +rf=298.257223563 +towgs84=0.000,0.000,0.000 +type=crs", ] # List attributes available in Darwin Core # not all attributes are returned in each request # to avoid key errors when accessing the dictionary returned by pygbif # presence of DWC keys in the returned dictionary is checked using this list # The number of keys in this list has to be equal to the number of columns # in the attribute table and the attributes written for each occurrence dwc_keys = [ "key", "taxonRank", "taxonKey", "taxonID", "scientificName", "species", "speciesKey", "genericName", "genus", "genusKey", "family", "familyKey", "order", "orderKey", "class", "classKey", "phylum", "phylumKey", "kingdom", "kingdomKey", "eventDate", "verbatimEventDate", "startDayOfYear", "endDayOfYear", "year", "month", "day", "occurrenceID", "occurrenceStatus", "occurrenceRemarks", "Habitat", "basisOfRecord", "preparations", "sex", "type", "locality", "verbatimLocality", "decimalLongitude", "decimalLatitude", "coordinateUncertaintyInMeters", "geodeticDatum", "higerGeography", "continent", "country", "countryCode", "stateProvince", "gbifID", "protocol", "identifier", "recordedBy", "identificationID", "identifiers", "dateIdentified", "modified", "institutionCode", "lastInterpreted", "lastParsed", "references", "relations", "catalogNumber", "occurrenceDetails", "datasetKey", "datasetName", "collectionCode", "rights", "rightsHolder", "license", "publishingOrgKey", "publishingCountry", "lastCrawled", "specificEpithet", "facts", "issues", "extensions", "language", ] # Deinfe columns for attribute table cols = [ ("cat", "INTEGER PRIMARY KEY"), ("g_search", "varchar(100)"), ("g_key", "integer"), ("g_taxonrank", "varchar(50)"), ("g_taxonkey", "integer"), ("g_taxonid", "varchar(50)"), ("g_scientificname", "varchar(255)"), ("g_species", "varchar(255)"), ("g_specieskey", "integer"), ("g_genericname", "varchar(255)"), ("g_genus", "varchar(50)"), ("g_genuskey", "integer"), ("g_family", "varchar(50)"), ("g_familykey", "integer"), ("g_order", "varchar(50)"), ("g_orderkey", "integer"), ("g_class", "varchar(50)"), ("g_classkey", "integer"), ("g_phylum", "varchar(50)"), ("g_phylumkey", "integer"), ("g_kingdom", "varchar(50)"), ("g_kingdomkey", "integer"), ("g_eventdate", "text"), ("g_verbatimeventdate", "varchar(50)"), ("g_startDayOfYear", "integer"), ("g_endDayOfYear", "integer"), ("g_year", "integer"), ("g_month", "integer"), ("g_day", "integer"), ("g_occurrenceid", "varchar(255)"), ("g_occurrenceStatus", "varchar(50)"), ("g_occurrenceRemarks", "varchar(50)"), ("g_Habitat", "varchar(50)"), ("g_basisofrecord", "varchar(50)"), ("g_preparations", "varchar(50)"), ("g_sex", "varchar(50)"), ("g_type", "varchar(50)"), ("g_locality", "varchar(255)"), ("g_verbatimlocality", "varchar(255)"), ("g_decimallongitude", "double precision"), ("g_decimallatitude", "double precision"), ("g_coordinateUncertaintyInMeters", "double precision"), ("g_geodeticdatum", "varchar(50)"), ("g_higerGeography", "varchar(255)"), ("g_continent", "varchar(50)"), ("g_country", "varchar(50)"), ("g_countryCode", "varchar(50)"), ("g_stateProvince", "varchar(50)"), ("g_gbifid", "varchar(255)"), ("g_protocol", "varchar(255)"), ("g_identifier", "varchar(50)"), ("g_recordedby", "varchar(255)"), ("g_identificationid", "varchar(255)"), ("g_identifiers", "text"), ("g_dateidentified", "text"), ("g_modified", "text"), ("g_institutioncode", "varchar(50)"), ("g_lastinterpreted", "text"), ("g_lastparsed", "text"), ("g_references", "varchar(255)"), ("g_relations", "text"), ("g_catalognumber", "varchar(50)"), ("g_occurrencedetails", "text"), ("g_datasetkey", "varchar(50)"), ("g_datasetname", "varchar(255)"), ("g_collectioncode", "varchar(50)"), ("g_rights", "varchar(255)"), ("g_rightsholder", "varchar(255)"), ("g_license", "varchar(50)"), ("g_publishingorgkey", "varchar(50)"), ("g_publishingcountry", "varchar(50)"), ("g_lastcrawled", "text"), ("g_specificepithet", "varchar(50)"), ("g_facts", "text"), ("g_issues", "text"), ("g_extensions", "text"), ("g_language", "varchar(50)"), ] # maybe no longer required in Python3 set_output_encoding() # Set temporal filter if requested by user # Initialize eventDate filter eventDate = None # Check if date from is compatible (ISO compliant) if date_from: try: parse(date_from) except: grass.fatal("Invalid invalid start date provided") if date_from and not date_to: eventDate = "{}".format(date_from) # Check if date to is compatible (ISO compliant) if date_to: try: parse(date_to) except: grass.fatal("Invalid invalid end date provided") # Check if date to is after date_from if parse(date_from) < parse(date_to): eventDate = "{},{}".format(date_from, date_to) else: grass.fatal( "Invalid date range: End date has to be after start date!") # Set filter on basisOfRecord if requested by user if basisofrecord == "ALL": basisOfRecord = None else: basisOfRecord = basisofrecord # Allow also occurrences with spatial issues if requested by user hasGeospatialIssue = False if hasGeoIssue: hasGeospatialIssue = True # Allow also occurrences without coordinates if requested by user hasCoordinate = True if allow_no_geom: hasCoordinate = False # Set reprojection parameters # Set target projection of current LOCATION proj_info = grass.parse_command("g.proj", flags="g") target_crs = grass.read_command("g.proj", flags="fj").rstrip() target = osr.SpatialReference() # Prefer EPSG CRS definitions if proj_info["epsg"]: target.ImportFromEPSG(int(proj_info["epsg"])) else: target.ImportFromProj4(target_crs) # GDAL >= 3 swaps x and y axis, see: github.com/gdal/issues/1546 if int(gdal_version[0]) >= 3: target.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER) if target_crs == "XY location (unprojected)": grass.fatal("Sorry, XY locations are not supported!") # Set source projection from GBIF source = osr.SpatialReference() source.ImportFromEPSG(4326) # GDAL >= 3 swaps x and y axis, see: github.com/gdal/issues/1546 if int(gdal_version[0]) >= 3: source.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER) if target_crs not in latlon_crs: transform = osr.CoordinateTransformation(source, target) reverse_transform = osr.CoordinateTransformation(target, source) # Generate WKT polygon to use for spatial filtering if requested if mask: if len(mask.split("@")) == 2: m = VectorTopo(mask.split("@")[0], mapset=mask.split("@")[1]) else: m = VectorTopo(mask) if not m.exist(): grass.fatal("Could not find vector map <{}>".format(mask)) m.open("r") if not m.is_open(): grass.fatal("Could not open vector map <{}>".format(mask)) # Use map Bbox as spatial filter if map contains <> 1 area if m.number_of("areas") == 1: region_pol = [area.to_wkt() for area in m.viter("areas")][0] else: bbox = (str(m.bbox()).replace("Bbox(", "").replace( " ", "").rstrip(")").split(",")) region_pol = "POLYGON (({0} {1}, {0} {3}, {2} {3}, {2} {1}, {0} {1}))".format( bbox[2], bbox[0], bbox[3], bbox[1]) m.close() else: # Do not limit import spatially if LOCATION is able to take global data if no_region_limit: if target_crs not in latlon_crs: grass.fatal("Import of data from outside the current region is" "only supported in a WGS84 location!") region_pol = None else: # Limit import spatially to current region # if LOCATION is !NOT! able to take global data # to avoid pprojection ERRORS region = grass.parse_command("g.region", flags="g") region_pol = "POLYGON (({0} {1},{0} {3},{2} {3},{2} {1},{0} {1}))".format( region["e"], region["n"], region["w"], region["s"]) # Do not reproject in latlon LOCATIONS if target_crs not in latlon_crs: pol = ogr.CreateGeometryFromWkt(region_pol) pol.Transform(reverse_transform) pol = pol.ExportToWkt() else: pol = region_pol # Create output map if not output maps for each species are requested if (not species_maps and not print_species and not print_species_shell and not print_occ_number and not print_species_table): mapname = output new = Vector(mapname) new.open("w", tab_name=mapname, tab_cols=cols) cat = 1 # Import data for each species for s in taxa_list: # Get the taxon key if not the taxon key is provided as input try: key = int(s) except: try: species_match = species.name_backbone(s, rank=rank, strict=False, verbose=True) key = species_match["usageKey"] except: grass.error( "Data request for taxon {} failed. Are you online?".format( s)) continue # Return matching taxon and alternatives and exit if print_species: print("Matching taxon for {} is:".format(s)) print("{} {}".format(species_match["scientificName"], species_match["status"])) if "alternatives" in list(species_match.keys()): print("Alternative matches might be: {}".format(s)) for m in species_match["alternatives"]: print("{} {}".format(m["scientificName"], m["status"])) else: print("No alternatives found for the given taxon") continue if print_species_shell: print("match={}".format(species_match["scientificName"])) if "alternatives" in list(species_match.keys()): alternatives = [] for m in species_match["alternatives"]: alternatives.append(m["scientificName"]) print("alternatives={}".format(",".join(alternatives))) continue if print_species_table: if "alternatives" in list(species_match.keys()): if len(species_match["alternatives"]) == 0: print("{0}|{1}|{2}|".format( s, key, species_match["scientificName"])) else: alternatives = [] for m in species_match["alternatives"]: alternatives.append(m["scientificName"]) print("{0}|{1}|{2}|{3}".format( s, key, species_match["scientificName"], ",".join(alternatives), )) continue try: returns_n = occurrences.search( taxonKey=key, hasGeospatialIssue=hasGeospatialIssue, hasCoordinate=hasCoordinate, institutionCode=institutionCode, basisOfRecord=basisOfRecord, recordedBy=recordedby, eventDate=eventDate, continent=continent, country=country, geometry=pol, limit=1, )["count"] except: grass.error( "Data request for taxon {} faild. Are you online?".format(s)) returns_n = 0 # Exit if search does not give a return # Print only number of returns for the given search and exit if print_occ_number: print("Found {0} occurrences for taxon {1}...".format( returns_n, s)) continue elif returns_n <= 0: grass.warning( "No occurrences for current search for taxon {0}...".format(s)) continue elif returns_n >= 200000: grass.warning( "Your search for {1} returns {0} records.\n" "Unfortunately, the GBIF search API is limited to 200,000 records per request.\n" "The download will be incomplete. Please consider to split up your search." .format(returns_n, s)) # Get the number of chunks to download chunks = int(math.ceil(returns_n / float(chunk_size))) grass.verbose("Downloading {0} occurrences for taxon {1}...".format( returns_n, s)) # Create a map for each species if requested using map name as suffix if species_maps: mapname = "{}_{}".format(s.replace(" ", "_"), output) new = Vector(mapname) new.open("w", tab_name=mapname, tab_cols=cols) cat = 0 # Download the data from GBIF for c in range(chunks): # Define offset offset = c * chunk_size # Adjust chunk_size to the hard limit of 200,000 records in GBIF API # if necessary if offset + chunk_size >= 200000: chunk_size = 200000 - offset # Get the returns for the next chunk returns = occurrences.search( taxonKey=key, hasGeospatialIssue=hasGeospatialIssue, hasCoordinate=hasCoordinate, institutionCode=institutionCode, basisOfRecord=basisOfRecord, recordedBy=recordedby, eventDate=eventDate, continent=continent, country=country, geometry=pol, limit=chunk_size, offset=offset, ) # Write the returned data to map and attribute table for res in returns["results"]: if target_crs not in latlon_crs: point = ogr.CreateGeometryFromWkt("POINT ({} {})".format( res["decimalLongitude"], res["decimalLatitude"])) point.Transform(transform) x = point.GetX() y = point.GetY() else: x = res["decimalLongitude"] y = res["decimalLatitude"] point = Point(x, y) for k in dwc_keys: if k not in list(res.keys()): res.update({k: None}) cat = cat + 1 new.write( point, cat=cat, attrs=( "{}".format(s), res["key"], res["taxonRank"], res["taxonKey"], res["taxonID"], res["scientificName"], res["species"], res["speciesKey"], res["genericName"], res["genus"], res["genusKey"], res["family"], res["familyKey"], res["order"], res["orderKey"], res["class"], res["classKey"], res["phylum"], res["phylumKey"], res["kingdom"], res["kingdomKey"], "{}".format(res["eventDate"]) if res["eventDate"] else None, "{}".format(res["verbatimEventDate"]) if res["verbatimEventDate"] else None, res["startDayOfYear"], res["endDayOfYear"], res["year"], res["month"], res["day"], res["occurrenceID"], res["occurrenceStatus"], res["occurrenceRemarks"], res["Habitat"], res["basisOfRecord"], res["preparations"], res["sex"], res["type"], res["locality"], res["verbatimLocality"], res["decimalLongitude"], res["decimalLatitude"], res["coordinateUncertaintyInMeters"], res["geodeticDatum"], res["higerGeography"], res["continent"], res["country"], res["countryCode"], res["stateProvince"], res["gbifID"], res["protocol"], res["identifier"], res["recordedBy"], res["identificationID"], ",".join(res["identifiers"]), "{}".format(res["dateIdentified"]) if res["dateIdentified"] else None, "{}".format(res["modified"]) if res["modified"] else None, res["institutionCode"], "{}".format(res["lastInterpreted"]) if res["lastInterpreted"] else None, "{}".format(res["lastParsed"]) if res["lastParsed"] else None, res["references"], ",".join(res["relations"]), res["catalogNumber"], "{}".format(res["occurrenceDetails"]) if res["occurrenceDetails"] else None, res["datasetKey"], res["datasetName"], res["collectionCode"], res["rights"], res["rightsHolder"], res["license"], res["publishingOrgKey"], res["publishingCountry"], "{}".format(res["lastCrawled"]) if res["lastCrawled"] else None, res["specificEpithet"], ",".join(res["facts"]), ",".join(res["issues"]), ",".join(res["extensions"]), res["language"], ), ) cat = cat + 1 # Close the current map if a map for each species is requested if species_maps: new.table.conn.commit() new.close() if not no_topo: grass.run_command("v.build", map=mapname, option="build") # Write history to map grass.vector_history(mapname) # Close the output map if not a map for each species is requested if (not species_maps and not print_species and not print_species_shell and not print_occ_number and not print_species_table): new.table.conn.commit() new.close() if not no_topo: grass.run_command("v.build", map=mapname, option="build") # Write history to map grass.vector_history(mapname)
def GetCentreAndExtentOfRaster(DataDirectory, RasterFile): """ This function takes a raster and returns the centrepoint and the extent in both degrees and metres. Args: DataDirectory (str): the data directory with the basin raster RasterFile (str): the name of the raster Returns: The lat-long of the centrepoint, the x-y- extent in both degrees and metres Author: SMM Date: 01/02/2018 """ print("Trying to create a shapefile.") print("The Data directory is: " + DataDirectory + " and the raster is: " + RasterFile) driver_name = "ESRI shapefile" driver = ogr.GetDriverByName(driver_name) # get the filename of the outfile. if not DataDirectory.endswith(os.sep): print( "You forgot the separator at the end of the directory, appending..." ) DataDirectory = DataDirectory + os.sep # Get the raster prefix SplitRasterfile = RasterFile.split(".") RasterPrefix = SplitRasterfile[0] # get the espg of the raster FullFilename = DataDirectory + RasterFile ESPG_this_raster = GetUTMEPSG(FullFilename) ESPG_this_raster = str(ESPG_this_raster) print("The raster has coordinate of: " + ESPG_this_raster) ESPG_this_raster_split = ESPG_this_raster.split(":") ESPG_this_raster = ESPG_this_raster_split[-1] print("This ESPG is: " + str(ESPG_this_raster)) # Get extent of raster [xmin, xmax, ymin, ymax] = GetRasterExtent(FullFilename) xproj_extent = xmax - xmin yproj_extent = ymax - ymin # Create ring ring = ogr.Geometry(ogr.wkbLinearRing) ring.AddPoint(xmin, ymin) ring.AddPoint(xmin, ymax) ring.AddPoint(xmax, ymax) ring.AddPoint(xmax, ymin) # Create a coordinate transformation source = osr.SpatialReference() source.ImportFromEPSG(int(ESPG_this_raster)) target = osr.SpatialReference() target.ImportFromEPSG(4326) transform = osr.CoordinateTransformation(source, target) # now transform the ring so you can get coordinates in lat-long ring.Transform(transform) # now get the xmin,ymin, and xmax, ymax coords in lat-long pt1 = ring.GetPoint(0) min_long = pt1[0] min_lat = pt1[1] pt2 = ring.GetPoint(2) max_long = pt2[0] max_lat = pt2[1] extent_long = max_long - min_long extent_lat = max_lat - min_lat centre_long = min_long + extent_long * 0.5 centre_lat = min_lat + extent_lat * 0.5 return centre_lat, centre_long, extent_lat, extent_long, xproj_extent, yproj_extent
def move(filename, t_srs, s_srs=None, pixel_threshold=None): # ------------------------------------------------------------------------- # Open the file. # ------------------------------------------------------------------------- ds = gdal.Open(filename) # ------------------------------------------------------------------------- # Compute the current (s_srs) locations of the four corners and center # of the image. # ------------------------------------------------------------------------- corners_names = [ 'Upper Left', 'Lower Left', 'Upper Right', 'Lower Right', 'Center' ] corners_pixel_line = [(0, 0, 0), (0, ds.RasterYSize, 0), (ds.RasterXSize, 0, 0), (ds.RasterXSize, ds.RasterYSize, 0), (ds.RasterXSize / 2.0, ds.RasterYSize / 2.0, 0.0)] orig_gt = ds.GetGeoTransform() corners_s_geo = [] for item in corners_pixel_line: corners_s_geo.append( (orig_gt[0] + item[0] * orig_gt[1] + item[1] * orig_gt[2], orig_gt[3] + item[0] * orig_gt[4] + item[1] * orig_gt[5], item[2])) # ------------------------------------------------------------------------- # Prepare a transformation from source to destination srs. # ------------------------------------------------------------------------- if s_srs is None: s_srs = ds.GetProjectionRef() s_srs_obj = osr.SpatialReference() s_srs_obj.SetFromUserInput(s_srs) t_srs_obj = osr.SpatialReference() t_srs_obj.SetFromUserInput(t_srs) tr = osr.CoordinateTransformation(s_srs_obj, t_srs_obj) # ------------------------------------------------------------------------- # Transform the corners # ------------------------------------------------------------------------- corners_t_geo = tr.TransformPoints(corners_s_geo) # ------------------------------------------------------------------------- # Compute a new geotransform for the image in the target SRS. For now # we just use the top left, top right, and bottom left to produce the # geotransform. The result will be exact at these three points by # definition, but if the underlying transformation is not affine it will # be wrong at the center and bottom right. It would be better if we # used all five points for a least squares fit but that is a bit beyond # me for now. # ------------------------------------------------------------------------- ul = corners_t_geo[0] ur = corners_t_geo[2] ll = corners_t_geo[1] new_gt = (ul[0], (ur[0] - ul[0]) / ds.RasterXSize, (ll[0] - ul[0]) / ds.RasterYSize, ul[1], (ur[1] - ul[1]) / ds.RasterXSize, (ll[1] - ul[1]) / ds.RasterYSize) inv_new_gt = gdal.InvGeoTransform(new_gt) # ------------------------------------------------------------------------- # Report results for the five locations. # ------------------------------------------------------------------------- corners_t_new_geo = [] error_geo = [] error_pixel_line = [] corners_pixel_line_new = [] print( '___Corner___ ________Original________ _______Adjusted_________ ______ Err (geo) ______ _Err (pix)_' ) for i in range(len(corners_s_geo)): # pylint: disable=consider-using-enumerate item = corners_pixel_line[i] corners_t_new_geo.append( (new_gt[0] + item[0] * new_gt[1] + item[1] * new_gt[2], new_gt[3] + item[0] * new_gt[4] + item[1] * new_gt[5], item[2])) error_geo.append((corners_t_new_geo[i][0] - corners_t_geo[i][0], corners_t_new_geo[i][1] - corners_t_geo[i][1], 0.0)) item = corners_t_geo[i] corners_pixel_line_new.append( (inv_new_gt[0] + item[0] * inv_new_gt[1] + item[1] * inv_new_gt[2], inv_new_gt[3] + item[0] * inv_new_gt[4] + item[1] * inv_new_gt[5], item[2])) error_pixel_line.append( (corners_pixel_line_new[i][0] - corners_pixel_line[i][0], corners_pixel_line_new[i][1] - corners_pixel_line[i][1], corners_pixel_line_new[i][2] - corners_pixel_line[i][2])) print('%-11s %s %s %s %5.2f %5.2f' % (corners_names[i], fmt_loc(s_srs_obj, corners_s_geo[i]), fmt_loc(t_srs_obj, corners_t_geo[i]), fmt_loc(t_srs_obj, error_geo[i]), error_pixel_line[i][0], error_pixel_line[i][1])) print('') # ------------------------------------------------------------------------- # Do we want to update the file? # ------------------------------------------------------------------------- max_error = 0 for err_item in error_pixel_line: this_error = math.sqrt(err_item[0] * err_item[0] + err_item[1] * err_item[1]) if this_error > max_error: max_error = this_error update = False if pixel_threshold is not None: if pixel_threshold > max_error: update = True # ------------------------------------------------------------------------- # Apply the change coordinate system and geotransform. # ------------------------------------------------------------------------- if update: ds = None ds = gdal.Open(filename, gdal.GA_Update) print('Updating file...') ds.SetGeoTransform(new_gt) ds.SetProjection(t_srs_obj.ExportToWkt()) print('Done.') elif pixel_threshold is None: print( 'No error threshold in pixels selected with -et, file not updated.' ) else: print("""Maximum check point error is %.5f pixels which exceeds the error threshold so the file has not been updated.""" % max_error) ds = None
def CreateShapefileOfRasterFootprint(DataDirectory, RasterFile): """ This function takes a raster and creates a shapefile that is the footprint of the raster. Used for plotting the raster footprint on regional maps using basemap. Variously put together from: http://osgeo-org.1560.x6.nabble.com/gdal-dev-Creating-a-simple-shapefile-with-ogr-td3749101.html Args: DataDirectory (str): the data directory with the basin raster RasterFile (str): the name of the raster Returns: Shapefile of the raster footprint Author: SMM Date: 23/01/2018 """ print("Trying to create a shapefile.") print("The Data directory is: " + DataDirectory + " and the raster is: " + RasterFile) driver_name = "ESRI shapefile" driver = ogr.GetDriverByName(driver_name) # get the filename of the outfile. if not DataDirectory.endswith(os.sep): print( "You forgot the separator at the end of the directory, appending..." ) DataDirectory = DataDirectory + os.sep # Get the raster prefix SplitRasterfile = RasterFile.split(".") RasterPrefix = SplitRasterfile[0] # get the espg of the raster FullFilename = DataDirectory + RasterFile ESPG_this_raster = GetUTMEPSG(FullFilename) ESPG_this_raster = str(ESPG_this_raster) print("The raster has coordinate of: " + ESPG_this_raster) ESPG_this_raster_split = ESPG_this_raster.split(":") ESPG_this_raster = ESPG_this_raster_split[-1] print("This ESPG is: " + str(ESPG_this_raster)) # Get extent of raster [xmin, xmax, ymin, ymax] = GetRasterExtent(FullFilename) # Create ring ring = ogr.Geometry(ogr.wkbLinearRing) ring.AddPoint(xmin, ymin) ring.AddPoint(xmin, ymax) ring.AddPoint(xmax, ymax) ring.AddPoint(xmax, ymin) ring.AddPoint(xmin, ymin) # Create polygon poly = ogr.Geometry(ogr.wkbPolygon) poly.AddGeometry(ring) # Create a coordinate transformation source = osr.SpatialReference() source.ImportFromEPSG(int(ESPG_this_raster)) target = osr.SpatialReference() target.ImportFromEPSG(4326) transform = osr.CoordinateTransformation(source, target) # now transformt the polygon poly.Transform(transform) # see what you got #print("The polygon is:") #print(poly.ExportToWkt()) # create the data source OutFileName = DataDirectory + RasterPrefix + "_footprint.shp" print("The output shapefile is: " + OutFileName) datasource = driver.CreateDataSource(OutFileName) # create the layer layer = datasource.CreateLayer(OutFileName, target, ogr.wkbPolygon) feature = ogr.Feature(layer.GetLayerDefn()) feature.SetGeometry(poly) layer.CreateFeature(feature) # Clean up feature.Destroy() datasource.Destroy()
def import_data(shapefile): fd, fname = tempfile.mkstemp(suffix=".zip") os.close(fd) f = open(fname, "wb") for chunk in shapefile.chunks(): f.write(chunk) f.close() if not zipfile.is_zipfile(fname): os.remove(fname) return "Not a valid zip archive." zip = zipfile.ZipFile(fname) required_suffixes = [".shp", ".shx", ".dbf", ".prj"] has_suffix = {} for suffix in required_suffixes: has_suffix[suffix] = False for info in zip.infolist(): suffix = os.path.splitext(info.filename)[1].lower() if suffix in required_suffixes: has_suffix[suffix] = True for suffix in required_suffixes: if not has_suffix[suffix]: zip.close() os.remove(fname) return "Archive missing required " + suffix + " file." shapefile_name = None dir_name = tempfile.mkdtemp() for info in zip.infolist(): if (info.filename.endswith(".shp") == True): shapefile_name = info.filename dst_file = os.path.join(dir_name, info.filename) f = open(dst_file, "wb") f.write(zip.read(info.filename)) f.close() zip.close() try: datasource = ogr.Open(os.path.join(dir_name, shapefile_name)) layer = datasource.GetLayer(0) ds = DataSource(os.path.join(dir_name, shapefile_name)) lyr = ds[0] shapefile_ok = True except: traceback.print_exc() shapefile_ok = False if not shapefile_ok: os.remove(fname) shutil.rmtree(dir_name) return "Not a valid shapefile." src_spatial_ref = layer.GetSpatialRef() print(src_spatial_ref) geom_type = layer.GetLayerDefn().GetGeomType() geom_name = ogr.GeometryTypeToName(geom_type) shapefile = Shapefile(filename=shapefile_name, srs_wkt=src_spatial_ref.ExportToWkt(), geom_type=geom_name) shapefile.save() attributes = [] layer_def = layer.GetLayerDefn() for i in range(layer_def.GetFieldCount()): field_def = layer_def.GetFieldDefn(i) attr = Attribute(shapefile=shapefile, name=field_def.GetName(), type=field_def.GetType(), width=field_def.GetWidth(), precision=field_def.GetPrecision()) attr.save() attributes.append(attr) dst_spatial_ref = osr.SpatialReference() dst_spatial_ref.ImportFromEPSG(4326) coord_transform = osr.CoordinateTransformation(src_spatial_ref, dst_spatial_ref) for i in range(layer.GetFeatureCount()): src_feature = layer.GetFeature(i) src_geometry = src_feature.GetGeometryRef() # src_geometry.Transform(coord_transform) geometry = GEOSGeometry(src_geometry.ExportToWkt()) geometry = utils.wrap_geos_geometry(geometry) geom_field = utils.calc_geometry_field(geom_name) fields = {} fields['shapefile'] = shapefile fields['name'] = 'GAPA_NAPA' fields[geom_field] = geometry feature = Feature(**fields) feature.save() for attr in attributes: success, result = utils.get_ogr_feature_attribute(attr, src_feature) if not success: os.remove(fname) shutil.rmtree(dir_name) shapefile.delete() return result attr_value = AttributeValue(feature=feature, attribute=attr, value=result) attr_value.save() os.remove(fname) shutil.rmtree(dir_name, ignore_errors=True, onerror=None) return None
def ogr_source_to_csv(source_definition, source_path, dest_path): "Convert a single shapefile or GeoJSON in source_path and put it in dest_path" in_datasource = ogr.Open(source_path, 0) in_layer = in_datasource.GetLayer() inSpatialRef = in_layer.GetSpatialRef() _L.info("Converting a layer to CSV: %s", in_layer.GetName()) # Determine the appropriate SRS if inSpatialRef is None: # OGR couldn't find the projection, let's hope there's an SRS tag. _L.info("No projection file found for source %s", source_path) srs = source_definition["conform"].get("srs", None) if srs is not None and srs.startswith(u"EPSG:"): _L.debug("SRS tag found specifying %s", srs) inSpatialRef = osr.SpatialReference() inSpatialRef.ImportFromEPSG(int(srs[5:])) else: # OGR is capable of doing more than EPSG, but so far we don't need it. raise Exception("Bad SRS. Can only handle EPSG, the SRS tag is %s", srs) # Determine the appropriate text encoding. This is complicated in OGR, see # https://github.com/openaddresses/machine/issues/42 if in_layer.TestCapability(ogr.OLCStringsAsUTF8): # OGR turned this to UTF 8 for us shp_encoding = 'utf-8' elif "encoding" in source_definition["conform"]: shp_encoding = source_definition["conform"]["encoding"] else: _L.warn( "No encoding given and OGR couldn't guess. Trying ISO-8859-1, YOLO!" ) shp_encoding = "iso-8859-1" _L.debug("Assuming shapefile data is encoded %s", shp_encoding) # Get the input schema, create an output schema in_layer_defn = in_layer.GetLayerDefn() out_fieldnames = [] for i in range(0, in_layer_defn.GetFieldCount()): field_defn = in_layer_defn.GetFieldDefn(i) out_fieldnames.append(field_defn.GetName()) out_fieldnames.append(X_FIELDNAME) out_fieldnames.append(Y_FIELDNAME) # Set up a transformation from the source SRS to EPSG:4326 outSpatialRef = osr.SpatialReference() outSpatialRef.ImportFromEPSG(4326) coordTransform = osr.CoordinateTransformation(inSpatialRef, outSpatialRef) # Write a CSV file with one row per feature in the OGR source with csvopen(dest_path, 'w', encoding='utf-8') as f: writer = csvDictWriter(f, fieldnames=out_fieldnames, encoding='utf-8') writer.writeheader() in_feature = in_layer.GetNextFeature() while in_feature: row = dict() for i in range(0, in_layer_defn.GetFieldCount()): field_defn = in_layer_defn.GetFieldDefn(i) field_value = in_feature.GetField(i) if isinstance(field_value, str): # Convert OGR's byte sequence strings to Python Unicode strings field_value = field_value.decode(shp_encoding) \ if hasattr(field_value, 'decode') else field_value row[field_defn.GetNameRef()] = field_value geom = in_feature.GetGeometryRef() if geom is not None: geom.Transform(coordTransform) # Calculate the centroid of the geometry and write it as X and Y columns try: centroid = geom.Centroid() except RuntimeError as e: if 'Invalid number of points in LinearRing found' not in str( e): raise xmin, xmax, ymin, ymax = geom.GetEnvelope() row[X_FIELDNAME] = xmin / 2 + xmax / 2 row[Y_FIELDNAME] = ymin / 2 + ymax / 2 else: row[X_FIELDNAME] = centroid.GetX() row[Y_FIELDNAME] = centroid.GetY() else: row[X_FIELDNAME] = None row[Y_FIELDNAME] = None writer.writerow(row) in_feature.Destroy() in_feature = in_layer.GetNextFeature() in_datasource.Destroy()
from osgeo import osr from osgeo import gdal DATA_PATH = "/Users/macbookair/PycharmProjects/helloworld/DATA/vector/" fic = DATA_PATH + "miami/miami.shp" # Définition de la projection source srcProjection = osr.SpatialReference() srcProjection.SetUTM(17) # Définition de la projection de destination dstProjection = osr.SpatialReference() dstProjection.SetWellKnownGeogCS('WGS84') # Lat/long. # Définition de la fonction de transformtion transform = osr.CoordinateTransformation(srcProjection, dstProjection) # Open the source shapefile. srcFile = ogr.Open(fic) srcLayer = srcFile.GetLayer(0) # Creation du shapefile avec son systeme de projection if os.path.exists("miami-reprojected"): shutil.rmtree("miami-reprojected") os.mkdir("miami-reprojected") driver = ogr.GetDriverByName("ESRI Shapefile") dstPath = os.path.join("miami-reprojected", "miami.shp") dstFile = driver.CreateDataSource(dstPath) dstLayer = dstFile.CreateLayer("layer", dstProjection)
def projectShpfileIntoXY(workDir, file0, inDirFilename, fn, typeOfGeom): # set the working directory #workDir = "E:\\Documents\\NutrientsLecture2\\Maps" os.chdir(workDir) # get the shapefile driver driver = ogr.GetDriverByName('ESRI Shapefile') # create the input SpatialReference inSpatialRef = osr.SpatialReference() inSpatialRef.ImportFromEPSG(4269) # Opening #file0 = "E:\\Red_deer_SPARROW\\RiverNet2\\CatchmentDef.shp" shfileCatch0, layer0 = openingShpFile(file0) # - Getting the spatial projection #geoSRlayer0 = layer0.GetSpatialRef() geoSR = layer0.GetSpatialRef() wkt = geoSR.ExportToWkt() print wkt# create the output SpatialReference outSpatialRef = osr.SpatialReference() outSpatialRef.ImportFromWkt(wkt) #geoSRop.ImportFromWkt(demWKT) # create the CoordinateTransformation coordTrans = osr.CoordinateTransformation(inSpatialRef, outSpatialRef) # open the input data source and get the layer #inDirFilename = r'E:\Documents\NutrientsLecture2\Maps\Catchment.shp' inDS = driver.Open(inDirFilename, 0) if inDS is None: print 'Could not open file' sys.exit(1) inLayer = inDS.GetLayer() # create a new data source and layer #fn = 'NLFLOW_1geog_MiryCreekV2_proj.shp' #fn = 'Catchment_proj.shp' if os.path.exists(fn): driver.DeleteDataSource(fn) outDS = driver.CreateDataSource(fn) if outDS is None: print 'Could not create file' sys.exit(1) #outLayer = outDS.CreateLayer('NLFLOW_1geog_MiryCreekV2_proj', outSpatialRef, geom_type=ogr.wkbLineString) if typeOfGeom == 'point': outLayer = outDS.CreateLayer(fn[:-4], outSpatialRef, geom_type=ogr.wkbPoint) elif typeOfGeom == 'line': outLayer = outDS.CreateLayer(fn[:-4], outSpatialRef, geom_type=ogr.wkbLineString) elif typeOfGeom == 'polygon': outLayer = outDS.CreateLayer(fn[:-4], outSpatialRef, geom_type=ogr.wkbPolygon) else: print 'Could not project file' sys.exit(1) #outLayer = outDS.CreateLayer(fn[:-4], outSpatialRef, geom_type=ogr.wkbPolygon) # Getting the fields of the features layer_defn = inLayer.GetLayerDefn() #get definitions of the layer field_names = [layer_defn.GetFieldDefn(i).GetName() for i in range(layer_defn.GetFieldCount())] #store the field names as a list of strings # use the input FieldDefn to add a field to the output for fieldname in field_names: fieldDefn = inLayer.GetFeature(0).GetFieldDefnRef(fieldname) outLayer.CreateField(fieldDefn) # # get the FieldDefn for the county name field # feature = inLayer.GetFeature(0) # fieldDefn = feature.GetFieldDefnRef('NID') # # add the field to the output shapefile # outLayer.CreateField(fieldDefn) # get the FeatureDefn for the output shapefile featureDefn = outLayer.GetLayerDefn() # loop through the input features inFeature = inLayer.GetNextFeature() k = 0 while inFeature: print 'Projection into XY coord feature No.: ', k # get the input geometry geom = inFeature.GetGeometryRef() #print geom.GetGeometryName() # Getting the type of geometry # reproject the geometry geom.Transform(coordTrans) # create a new feature outFeature = ogr.Feature(featureDefn) # set the geometry and attribute outFeature.SetGeometry(geom) # Set attribute for fieldname in field_names: outFeature.SetField(fieldname, inFeature.GetField(fieldname)) #outFeature.SetField('NID', inFeature.GetField('NID')) # add the feature to the shapefile outLayer.CreateFeature(outFeature) # destroy the features and get the next input feature outFeature.Destroy inFeature.Destroy inFeature = inLayer.GetNextFeature() k += 1 # Close the shapefiles inDS.Destroy() outDS.Destroy() # create the *.prj file outSpatialRef.MorphToESRI() #open('redDeerRiverNHN_proj.prj', 'w') file = open(fn[:-4]+'.prj','w')#open('redDeerRiverNHN_proj.prj', 'w') file.write(outSpatialRef.ExportToWkt()) file.close()
def xyz_parse(src_xyz, xyz_c=_xyz_config, region=None, verbose=False): """xyz file parsing generator Args: src_xyz (generataor): list/generator of xyz data xyz_c (dict): xyz config dictionary region (list): a `region` list [xmin, xmax, ymin, ymax] verbose (bool): increase verbosity Yields: list: xyz data [x, y, z, ...] """ ln = 0 pass_d = True skip = int(xyz_c['skip']) if xyz_c['epsg'] == xyz_c['warp'] or xyz_c['epsg'] is None: xyz_c['warp'] = None if xyz_c['warp'] is not None: src_srs = osr.SpatialReference() src_srs.ImportFromEPSG(int(xyz_c['epsg'])) dst_srs = osr.SpatialReference() dst_srs.ImportFromEPSG(int(xyz_c['warp'])) try: src_srs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER) dst_srs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER) except: pass dst_trans = osr.CoordinateTransformation(src_srs, dst_srs) else: src_srs = dst_srs = dst_trans = None for xyz in src_xyz: pass_d = True if ln >= skip: this_xyz = xyz_parse_line(xyz, xyz_c) if this_xyz is not None: if xyz_c['warp'] is not None: this_xyz = xyz_warp(this_xyz, dst_trans) if region is not None: if regions.region_valid_p: if not xyz_in_region_p(this_xyz, region): pass_d = False if xyz_c['upper_limit'] is not None or xyz_c[ 'lower_limit'] is not None: if not regions.z_pass(this_xyz[2], upper_limit=xyz_c['upper_limit'], lower_limit=xyz_c['lower_limit']): pass_d = False else: pass_d = False if pass_d: ln += 1 yield (this_xyz) else: skip -= 1 if verbose: if ln == 0: status = -1 else: status = 0 utils.echo_msg('parsed {} data records from {}'.format( ln, xyz_c['name']))
if substepsize > stepsize: substepsize = stepsize # - # Do we have an alternate SRS? ct = None if t_srs is not None: t_srs_o = osr.SpatialReference() t_srs_o.SetFromUserInput(t_srs) s_srs_o = osr.SpatialReference() s_srs_o.SetFromUserInput('WGS84') ct = osr.CoordinateTransformation(s_srs_o, t_srs_o) else: t_srs_o = osr.SpatialReference() t_srs_o.SetFromUserInput('WGS84') # - # Create graticule file. drv = ogr.GetDriverByName('ESRI Shapefile') try: drv.DeleteDataSource(outfile) except: pass ds = drv.CreateDataSource(outfile)
def main(): ### Read input arguments ##### logfilename = 'wtools_static_maps.log' parser = OptionParser() usage = "usage: %prog [options]" parser = OptionParser(usage=usage) parser.add_option('-q', '--quiet', dest='verbose', default=True, action='store_false', help='do not print status messages to stdout') parser.add_option('-i', '--ini', dest='inifile', default=None, help='ini file with settings for static_maps.exe') parser.add_option('-s', '--source', dest='source', default='wflow', help='Source folder containing clone (default=./wflow)') parser.add_option('-d', '--destination', dest='destination', default='staticmaps', help='Destination folder (default=./staticmaps)') parser.add_option('-r', '--river', dest='rivshp', default=None, help='river network polyline layer (ESRI Shapefile)') parser.add_option('-c', '--catchment', dest='catchshp', default=None, help='catchment polygon layer (ESRI Shapefile)') parser.add_option('-g', '--gauges', dest='gaugeshp', default=None, help='gauge point layer (ESRI Shapefile)') parser.add_option('-D', '--dem', dest='dem_in', default=None, help='digital elevation model (GeoTiff)') parser.add_option('-L', '--landuse', dest='landuse', default=None, help='land use / land cover layer (GeoTiff)') parser.add_option('-S', '--soiltype', dest='soil', default=None, help='soil type layer (GeoTiff)') parser.add_option('-V', '--vegetation', dest='lai', default=None, help='vegetation LAI layer location (containing 12 GeoTiffs <LAI00000.XXX.tif>)') parser.add_option('-O', '--other_maps', dest='other_maps', default=None, help='bracketed [] comma-separated list of paths to other maps that should be reprojected') parser.add_option('-C', '--clean', dest='clean', default=False, action='store_true', help='Clean the .xml files from static maps folder when finished') parser.add_option('-A', '--alltouch', dest='alltouch', default=False, action='store_true', help='option to burn catchments "all touching".\nUseful when catchment-size is small compared to cellsize') (options, args) = parser.parse_args() # parse other maps into an array options.other_maps = options.other_maps.replace(' ', '').replace('[', '').replace(']', '').split(',') options.source = os.path.abspath(options.source) clone_map = os.path.join(options.source, 'mask.map') clone_shp = os.path.join(options.source, 'mask.shp') clone_prj = os.path.join(options.source, 'mask.prj') if None in (options.inifile, options.rivshp, options.catchshp, options.dem_in): msg = """The following files are compulsory: - ini file - DEM (raster) - river (shape) - catchment (shape) """ print(msg) parser.print_help() sys.exit(1) if not os.path.exists(options.inifile): print 'path to ini file cannot be found' sys.exit(1) if not os.path.exists(options.rivshp): print 'path to river shape cannot be found' sys.exit(1) if not os.path.exists(options.catchshp): print 'path to catchment shape cannot be found' sys.exit(1) if not os.path.exists(options.dem_in): print 'path to DEM cannot be found' sys.exit(1) # open a logger, dependent on verbose print to screen or not logger, ch = wtools_lib.setlogger(logfilename, 'WTOOLS', options.verbose) # create directories # TODO: check if workdir is still necessary, try to keep in memory as much as possible # delete old files (when the source and destination folder are different) if np.logical_and(os.path.isdir(options.destination), options.destination is not options.source): shutil.rmtree(options.destination) if options.destination is not options.source: os.makedirs(options.destination) # Read mask if not(os.path.exists(clone_map)): logger.error('Clone file {:s} not found. Please run create_grid first.'.format(clone_map)) sys.exit(1) else: # set clone pcr.setclone(clone_map) # get the extent from clone.tif xax, yax, clone, fill_value = gis.gdal_readmap(clone_map, 'GTiff') trans = wtools_lib.get_geotransform(clone_map) extent = wtools_lib.get_extent(clone_map) xmin, ymin, xmax, ymax = extent zeros = np.zeros(clone.shape) ones = pcr.numpy2pcr(pcr.Scalar, np.ones(clone.shape), -9999) # get the projection from clone.tif srs = wtools_lib.get_projection(clone_map) unit_clone = srs.GetAttrValue('UNIT').lower() ### READ CONFIG FILE # open config-file config=wtools_lib.OpenConf(options.inifile) # read settings snapgaugestoriver = wtools_lib.configget(config, 'settings', 'snapgaugestoriver', True, datatype='boolean') burnalltouching = wtools_lib.configget(config, 'settings', 'burncatchalltouching', True, datatype='boolean') burninorder = wtools_lib.configget(config, 'settings', 'burncatchalltouching', False, datatype='boolean') verticetollerance = wtools_lib.configget(config, 'settings', 'vertice_tollerance', 0.0001, datatype='float') ''' read parameters ''' burn_outlets = wtools_lib.configget(config, 'parameters', 'burn_outlets', 10000, datatype='int') burn_rivers = wtools_lib.configget(config, 'parameters', 'burn_rivers', 200, datatype='int') burn_connections = wtools_lib.configget(config, 'parameters', 'burn_connections', 100, datatype='int') burn_gauges = wtools_lib.configget(config, 'parameters', 'burn_gauges', 100, datatype='int') minorder = wtools_lib.configget(config, 'parameters', 'riverorder_min', 3, datatype='int') percentiles = np.array( config.get('parameters', 'statisticmaps', '0, 100').replace( ' ', '').split(','), dtype='float') # read the parameters for generating a temporary very high resolution grid if unit_clone == 'degree': cellsize_hr = wtools_lib.configget(config, 'parameters', 'highres_degree', 0.0005, datatype='float') elif (unit_clone == 'metre') or (unit_clone == 'meter'): cellsize_hr = wtools_lib.configget(config, 'parameters', 'highres_metre', 50, datatype='float') cols_hr = int((float(xmax)-float(xmin))/cellsize_hr + 2) rows_hr = int((float(ymax)-float(ymin))/cellsize_hr + 2) hr_trans = (float(xmin), cellsize_hr, float(0), float(ymax), 0, -cellsize_hr) clone_hr = os.path.join(options.destination, 'clone_highres.tif') # make a highres clone as well! wtools_lib.CreateTif(clone_hr, rows_hr, cols_hr, hr_trans, srs, 0) # read staticmap locations catchment_map = wtools_lib.configget(config, 'staticmaps', 'catchment', 'wflow_catchment.map') dem_map = wtools_lib.configget(config, 'staticmaps', 'dem', 'wflow_dem.map') demmax_map = wtools_lib.configget(config, 'staticmaps', 'demmax', 'wflow_demmax.map') demmin_map = wtools_lib.configget(config, 'staticmaps', 'demmin', 'wflow_demmin.map') gauges_map = wtools_lib.configget(config, 'staticmaps', 'gauges', 'wflow_gauges.map') landuse_map = wtools_lib.configget(config, 'staticmaps', 'landuse', 'wflow_landuse.map') ldd_map = wtools_lib.configget(config, 'staticmaps', 'ldd', 'wflow_ldd.map') river_map = wtools_lib.configget(config, 'staticmaps', 'river', 'wflow_river.map') outlet_map = wtools_lib.configget(config, 'staticmaps', 'outlet', 'wflow_outlet.map') riverlength_fact_map = wtools_lib.configget(config, 'staticmaps', 'riverlength_fact', 'wflow_riverlength_fact.map') soil_map = wtools_lib.configget(config, 'staticmaps', 'soil', 'wflow_soil.map') streamorder_map = wtools_lib.configget(config, 'staticmaps', 'streamorder', 'wflow_streamorder.map') subcatch_map = wtools_lib.configget(config, 'staticmaps', 'subcatch', 'wflow_subcatch.map') # read mask location (optional) masklayer = wtools_lib.configget(config, 'mask', 'masklayer', options.catchshp) # ???? empty = pcr.ifthen(ones == 0, pcr.scalar(0)) # TODO: check if extents are correct this way # TODO: check what the role of missing values is in zeros and ones (l. 123 in old code) # first add a missing value to dem_in ds = gdal.Open(options.dem_in, gdal.GA_Update) RasterBand = ds.GetRasterBand(1) fill_val = RasterBand.GetNoDataValue() if fill_val is None: RasterBand.SetNoDataValue(-9999) ds = None # reproject to clone map: see http://stackoverflow.com/questions/10454316/how-to-project-and-resample-a-grid-to-match-another-grid-with-gdal-python # resample DEM logger.info('Resampling dem from {:s} to {:s}'.format(os.path.abspath(options.dem_in), os.path.join(options.destination, dem_map))) gis.gdal_warp(options.dem_in, clone_map, os.path.join(options.destination, dem_map), format='PCRaster', gdal_interp=gdalconst.GRA_Average) # retrieve amount of rows and columns from clone # TODO: make windowstats applicable to source/target with different projections. This does not work yet. # retrieve srs from DEM try: srs_dem = wtools_lib.get_projection(options.dem_in) except: logger.warning('No projection found in DEM, assuming WGS 1984 lat long') srs_dem = osr.SpatialReference() srs_dem.ImportFromEPSG(4326) clone2dem_transform = osr.CoordinateTransformation(srs,srs_dem) #if srs.ExportToProj4() == srs_dem.ExportToProj4(): for percentile in percentiles: if percentile >= 100: logger.info('computing window maximum') percentile_dem = os.path.join(options.destination, 'wflow_dem_max.map') elif percentile <= 0: logger.info('computing window minimum') percentile_dem = os.path.join(options.destination, 'wflow_dem_min.map') else: logger.info('computing window {:d} percentile'.format(int(percentile))) percentile_dem = os.path.join(options.destination, 'wflow_dem_{:03d}.map'.format(int(percentile))) percentile_dem = os.path.join(options.destination, 'wflow_dem_{:03d}.map'.format(int(percentile))) stats = wtools_lib.windowstats(options.dem_in, len(yax), len(xax), trans, srs, percentile_dem, percentile, transform=clone2dem_transform,logger=logger) # else: # logger.warning('Projections of DEM and clone are different. DEM statistics for different projections is not yet implemented') """ # burn in rivers # first convert and clip the river shapefile # retrieve river shape projection, if not available assume EPSG:4326 file_att = os.path.splitext(os.path.basename(options.rivshp))[0] ds = ogr.Open(options.rivshp) lyr = ds.GetLayerByName(file_att) extent = lyr.GetExtent() extent_in = [extent[0], extent[2], extent[1], extent[3]] try: # get spatial reference from shapefile srs_rivshp = lyr.GetSpatialRef() logger.info('Projection in river shapefile is {:s}'.format(srs_rivshp.ExportToProj4())) except: logger.warning('No projection found in {:s}, assuming WGS 1984 lat-lon'.format(options.rivshp)) srs_rivshp = osr.SpatialReference() srs_rivshp.ImportFromEPSG(4326) rivprojshp = os.path.join(options.destination, 'rivshp_proj.shp') logger.info('Projecting and clipping {:s} to {:s}'.format(options.rivshp, rivprojshp)) # TODO: Line below takes a very long time to process, the bigger the shapefile, the more time. How do we deal with this? call(('ogr2ogr','-s_srs', srs_rivshp.ExportToProj4(),'-t_srs', srs.ExportToProj4(), '-clipsrc', '{:f}'.format(xmin), '{:f}'.format(ymin), '{:f}'.format(xmax), '{:f}'.format(ymax), rivprojshp, options.rivshp)) """ # TODO: BURNING!! # project catchment layer to projection of clone file_att = os.path.splitext(os.path.basename(options.catchshp))[0] print options.catchshp ds = ogr.Open(options.catchshp) lyr = ds.GetLayerByName(file_att) extent = lyr.GetExtent() extent_in = [extent[0], extent[2], extent[1], extent[3]] try: # get spatial reference from shapefile srs_catchshp = lyr.GetSpatialRef() logger.info('Projection in catchment shapefile is {:s}'.format(srs_catchshp.ExportToProj4())) except: logger.warning('No projection found in {:s}, assuming WGS 1984 lat-lon'.format(options.catchshp)) srs_catchshp = osr.SpatialReference() srs_catchshp.ImportFromEPSG(4326) catchprojshp = os.path.join(options.destination, 'catchshp_proj.shp') logger.info('Projecting {:s} to {:s}'.format(options.catchshp, catchprojshp)) call(('ogr2ogr','-s_srs', srs_catchshp.ExportToProj4(),'-t_srs', srs.ExportToProj4(), '-clipsrc', '{:f}'.format(xmin), '{:f}'.format(ymin), '{:f}'.format(xmax), '{:f}'.format(ymax), catchprojshp, options.catchshp)) # logger.info('Calculating ldd') ldddem = pcr.readmap(os.path.join(options.destination, dem_map)) ldd_select=pcr.lddcreate(ldddem, 1e35, 1e35, 1e35, 1e35) pcr.report(ldd_select, os.path.join(options.destination, 'wflow_ldd.map')) # compute stream order, identify river cells streamorder = pcr.ordinal(pcr.streamorder(ldd_select)) river = pcr.ifthen(streamorder >= pcr.ordinal(minorder), pcr.boolean(1)) # find the minimum value in the DEM and cover missing values with a river with this value. Effect is none!! so now left out! # mindem = int(np.min(pcr.pcr2numpy(pcr.ordinal(os.path.join(options.destination, dem_map)),9999999))) # dem_resample_map = pcr.cover(os.path.join(options.destination, dem_map), pcr.scalar(river)*0+mindem) # pcr.report(dem_resample_map, os.path.join(options.destination, dem_map)) pcr.report(streamorder, os.path.join(options.destination, streamorder_map)) pcr.report(river, os.path.join(options.destination, river_map)) # deal with your catchments if options.gaugeshp == None: logger.info('No gauges defined, using outlets instead') gauges = pcr.ordinal( pcr.uniqueid( pcr.boolean( pcr.ifthen(pcr.scalar(ldd_select)==5, pcr.boolean(1) ) ) ) ) pcr.report(gauges, os.path.join(options.destination, gauges_map)) # TODO: Add the gauge shape code from StaticMaps.py (line 454-489) # TODO: add river length map (see SticMaps.py, line 492-499) # report river length # make a high resolution empty map dem_hr_file = os.path.join(options.destination, 'dem_highres.tif') burn_hr_file = os.path.join(options.destination, 'burn_highres.tif') demburn_hr_file = os.path.join(options.destination, 'demburn_highres.map') riv_hr_file = os.path.join(options.destination, 'riv_highres.map') gis.gdal_warp(options.dem_in, clone_hr, dem_hr_file) # wtools_lib.CreateTif(riv_hr, rows_hr, cols_hr, hr_trans, srs, 0) file_att = os.path.splitext(os.path.basename(options.rivshp))[0] # open the shape layer ds = ogr.Open(options.rivshp) lyr = ds.GetLayerByName(file_att) gis.ogr_burn(lyr, clone_hr, -100, file_out=burn_hr_file, format='GTiff', gdal_type=gdal.GDT_Float32, fill_value=0) # read dem and burn values and add xax_hr, yax_hr, burn_hr, fill = gis.gdal_readmap(burn_hr_file, 'GTiff') burn_hr[burn_hr==fill] = 0 xax_hr, yax_hr, dem_hr, fill = gis.gdal_readmap(dem_hr_file, 'GTiff') dem_hr[dem_hr==fill] = np.nan demburn_hr = dem_hr + burn_hr demburn_hr[np.isnan(demburn_hr)] = -9999 gis.gdal_writemap(demburn_hr_file, 'PCRaster', xax_hr, yax_hr, demburn_hr, -9999.) pcr.setclone(demburn_hr_file) demburn_hr = pcr.readmap(demburn_hr_file) ldd_hr = pcr.lddcreate(demburn_hr, 1e35, 1e35, 1e35, 1e35) pcr.report(ldd_hr, os.path.join(options.destination, 'ldd_hr.map')) pcr.setglobaloption('unitcell') riv_hr = pcr.scalar(pcr.streamorder(ldd_hr) >= minorder)*pcr.downstreamdist(ldd_hr) pcr.report(riv_hr, riv_hr_file) pcr.setglobaloption('unittrue') pcr.setclone(clone_map) logger.info('Computing river length') #riverlength = wt.windowstats(riv_hr,clone_rows,clone_columns,clone_trans,srs_clone,resultdir,'frac',clone2dem_transform) riverlength = wtools_lib.windowstats(riv_hr_file, len(yax), len(xax), trans, srs, os.path.join(options.destination, riverlength_fact_map), stat='fact', logger=logger) # TODO: nothing happends with the river lengths yet. Need to decide how to use these # report outlet map pcr.report(pcr.ifthen(pcr.ordinal(ldd_select)==5, pcr.ordinal(1)), os.path.join(options.destination, outlet_map)) # report subcatchment map subcatchment = pcr.subcatchment(ldd_select, gauges) pcr.report(pcr.ordinal(subcatchment), os.path.join(options.destination, subcatch_map)) # Report land use map if options.landuse == None: logger.info('No land use map used. Preparing {:s} with only ones.'. format(os.path.join(options.destination, landuse_map))) pcr.report(pcr.nominal(ones), os.path.join(options.destination, landuse_map)) else: logger.info('Resampling land use from {:s} to {:s}'. format(os.path.abspath(options.landuse), os.path.join(options.destination, os.path.abspath(landuse_map)))) gis.gdal_warp(options.landuse, clone_map, os.path.join(options.destination, landuse_map), format='PCRaster', gdal_interp=gdalconst.GRA_Mode, gdal_type=gdalconst.GDT_Int32) # report soil map if options.soil == None: logger.info('No soil map used. Preparing {:s} with only ones.'. format(os.path.join(options.destination, soil_map))) pcr.report(pcr.nominal(ones), os.path.join(options.destination, soil_map)) else: logger.info('Resampling soil from {:s} to {:s}'. format(os.path.abspath(options.soil), os.path.join(options.destination, os.path.abspath(soil_map)))) gis.gdal_warp(options.soil, clone_map, os.path.join(options.destination, soil_map), format='PCRaster', gdal_interp=gdalconst.GRA_Mode, gdal_type=gdalconst.GDT_Int32) if options.lai == None: logger.info('No vegetation LAI maps used. Preparing default maps {:s} with only ones.'. format(os.path.join(options.destination, soil_map))) pcr.report(pcr.nominal(ones), os.path.join(options.destination, soil_map)) else: dest_lai = os.path.join(options.destination, 'clim') os.makedirs(dest_lai) for month in range(12): lai_in = os.path.join(options.lai, 'LAI00000.{:03d}'.format(month + 1)) lai_out = os.path.join(dest_lai, 'LAI00000.{:03d}'.format(month + 1)) logger.info('Resampling vegetation LAI from {:s} to {:s}'. format(os.path.abspath(lai_in), os.path.abspath(lai_out))) gis.gdal_warp(lai_in, clone_map, lai_out, format='PCRaster', gdal_interp=gdalconst.GRA_Bilinear, gdal_type=gdalconst.GDT_Float32) # report soil map if options.other_maps == None: logger.info('No other maps used. Skipping other maps.') else: logger.info('Resampling list of other maps...') for map_file in options.other_maps: map_name = os.path.split(map_file)[1] logger.info('Resampling a map from {:s} to {:s}'. format(os.path.abspath(map_file), os.path.join(options.destination, map_name))) gis.gdal_warp(map_file, clone_map, os.path.join(options.destination, map_name), format='PCRaster', gdal_interp=gdalconst.GRA_Mode, gdal_type=gdalconst.GDT_Float32) if options.clean: wtools_lib.DeleteList(glob.glob(os.path.join(options.destination, '*.xml')), logger=logger) wtools_lib.DeleteList(glob.glob(os.path.join(options.destination, 'clim', '*.xml')), logger=logger) wtools_lib.DeleteList(glob.glob(os.path.join(options.destination, '*highres*')), logger=logger)
def parse_shp(shp_file_path): """ :param shp_file_path: full file path fo the .shp file output dictionary format shp_metadata_dict["origin_projection_string"]: original projection string shp_metadata_dict["origin_projection_name"]: origin_projection_name shp_metadata_dict["origin_datum"]: origin_datum shp_metadata_dict["origin_unit"]: origin_unit shp_metadata_dict["field_meta_dict"]["field_list"]: list [fieldname1, fieldname2...] shp_metadata_dict["field_meta_dict"]["field_attr_dic"]: dict {"fieldname": dict { "fieldName":fieldName, "fieldTypeCode":fieldTypeCode, "fieldType":fieldType, "fieldWidth:fieldWidth, "fieldPrecision:fieldPrecision" } } shp_metadata_dict["feature_count"]: feature count shp_metadata_dict["geometry_type"]: geometry_type shp_metadata_dict["origin_extent_dict"]: dict{"west": east, "north":north, "east":east, "south":south} shp_metadata_dict["wgs84_extent_dict"]: dict{"west": east, "north":north, "east":east, "south":south} """ shp_metadata_dict = {} # read shapefile driver = ogr.GetDriverByName('ESRI Shapefile') dataset = driver.Open(shp_file_path) # get layer layer = dataset.GetLayer() # get spatialRef from layer spatialRef_from_layer = layer.GetSpatialRef() if spatialRef_from_layer is not None: shp_metadata_dict["origin_projection_string"] = str( spatialRef_from_layer) prj_name = spatialRef_from_layer.GetAttrValue('projcs') if prj_name is None: prj_name = spatialRef_from_layer.GetAttrValue('geogcs') shp_metadata_dict["origin_projection_name"] = prj_name shp_metadata_dict["origin_datum"] = spatialRef_from_layer.GetAttrValue( 'datum') shp_metadata_dict["origin_unit"] = spatialRef_from_layer.GetAttrValue( 'unit') else: shp_metadata_dict["origin_projection_string"] = UNKNOWN_STR shp_metadata_dict["origin_projection_name"] = UNKNOWN_STR shp_metadata_dict["origin_datum"] = UNKNOWN_STR shp_metadata_dict["origin_unit"] = UNKNOWN_STR field_list = [] filed_attr_dic = {} field_meta_dict = { "field_list": field_list, "field_attr_dict": filed_attr_dic } shp_metadata_dict["field_meta_dict"] = field_meta_dict # get Attributes layerDefinition = layer.GetLayerDefn() for i in range(layerDefinition.GetFieldCount()): fieldName = layerDefinition.GetFieldDefn(i).GetName() field_list.append(fieldName) attr_dict = {} field_meta_dict["field_attr_dict"][fieldName] = attr_dict attr_dict["fieldName"] = fieldName fieldTypeCode = layerDefinition.GetFieldDefn(i).GetType() attr_dict["fieldTypeCode"] = fieldTypeCode fieldType = layerDefinition.GetFieldDefn(i).GetFieldTypeName( fieldTypeCode) attr_dict["fieldType"] = fieldType fieldWidth = layerDefinition.GetFieldDefn(i).GetWidth() attr_dict["fieldWidth"] = fieldWidth fieldPrecision = layerDefinition.GetFieldDefn(i).GetPrecision() attr_dict["fieldPrecision"] = fieldPrecision # get layer extent layer_extent = layer.GetExtent() # get feature count featureCount = layer.GetFeatureCount() shp_metadata_dict["feature_count"] = featureCount # get a feature from layer feature = layer.GetNextFeature() # get geometry from feature geom = feature.GetGeometryRef() # get geometry name shp_metadata_dict["geometry_type"] = geom.GetGeometryName() # reproject layer extent # source SpatialReference source = spatialRef_from_layer # target SpatialReference target = osr.SpatialReference() target.ImportFromEPSG(4326) # create two key points from layer extent left_upper_point = ogr.Geometry(ogr.wkbPoint) left_upper_point.AddPoint(layer_extent[0], layer_extent[3]) # left-upper right_lower_point = ogr.Geometry(ogr.wkbPoint) right_lower_point.AddPoint(layer_extent[1], layer_extent[2]) # right-lower # source map always has extent, even projection is unknown shp_metadata_dict["origin_extent_dict"] = {} shp_metadata_dict["origin_extent_dict"]["westlimit"] = layer_extent[0] shp_metadata_dict["origin_extent_dict"]["northlimit"] = layer_extent[3] shp_metadata_dict["origin_extent_dict"]["eastlimit"] = layer_extent[1] shp_metadata_dict["origin_extent_dict"]["southlimit"] = layer_extent[2] # reproject to WGS84 shp_metadata_dict["wgs84_extent_dict"] = {} if source is not None: # define CoordinateTransformation obj transform = osr.CoordinateTransformation(source, target) # project two key points left_upper_point.Transform(transform) right_lower_point.Transform(transform) shp_metadata_dict["wgs84_extent_dict"][ "westlimit"] = left_upper_point.GetX() shp_metadata_dict["wgs84_extent_dict"][ "northlimit"] = left_upper_point.GetY() shp_metadata_dict["wgs84_extent_dict"][ "eastlimit"] = right_lower_point.GetX() shp_metadata_dict["wgs84_extent_dict"][ "southlimit"] = right_lower_point.GetY() shp_metadata_dict["wgs84_extent_dict"][ "projection"] = "WGS 84 EPSG:4326" shp_metadata_dict["wgs84_extent_dict"]["units"] = "Decimal degrees" else: shp_metadata_dict["wgs84_extent_dict"]["westlimit"] = UNKNOWN_STR shp_metadata_dict["wgs84_extent_dict"]["northlimit"] = UNKNOWN_STR shp_metadata_dict["wgs84_extent_dict"]["eastlimit"] = UNKNOWN_STR shp_metadata_dict["wgs84_extent_dict"]["southlimit"] = UNKNOWN_STR shp_metadata_dict["wgs84_extent_dict"]["projection"] = UNKNOWN_STR shp_metadata_dict["wgs84_extent_dict"]["units"] = UNKNOWN_STR return shp_metadata_dict
sys.exit(1) if radiusBand_num is not None: radiusBand = indataset.GetRasterBand(radiusBand_num) if radiusBand is None: print 'Could not get Radius band %d' % radiusBand_num sys.exit(1) geomatrix = indataset.GetGeoTransform() # Build Spatial Reference object based on coordinate system, fetched from the # opened dataset srs = osr.SpatialReference() srs.ImportFromWkt(indataset.GetProjection()) #print srs srsLatLong = srs.CloneGeogCS() coordtransform = osr.CoordinateTransformation(srs, srsLatLong) # Collect information on all the source files. if srcwin is None: srcwin = (0, 0, indataset.RasterXSize, indataset.RasterYSize) # Open the output file. if dstfile is not None: dst_fh = open(dstfile, 'wt') else: dst_fh = sys.stdout if addheader: if printLatLon: dst_fh.write("Lon,Lat,Band\n") else:
def compute_fields(self): """Other keyword args get passed in as a matter of course, like BBOX, time, and elevation, but this basic driver ignores them""" super(SpatialiteDriver, self).compute_fields() if not hasattr(self, "src_ext") and self.resource.resource_file: self.src_ext = self.resource.resource_file.split('.')[-1] # convert any other kind of file to spatialite. this way the sqlite driver can be used with any OGR compatible # file if self.src_ext.endswith('zip'): archive = ZipFile(self.cached_basename + self.src_ext) projection_found = False for name in archive.namelist(): xtn = name.split('.')[-1].lower() if xtn in {'shp', 'shx', 'dbf', 'prj' } and "__MACOSX" not in name: projection_found = projection_found or xtn == 'prj' with open(self.cached_basename + '.' + xtn, 'wb') as fout: with archive.open(name) as fin: chunk = fin.read(65536) while chunk: fout.write(chunk) chunk = fin.read(65536) if not projection_found: with open(self.cached_basename + '.prj', 'w') as f: srs = osr.SpatialReference() srs.ImportFromEPSG(4326) f.write(srs.ExportToWkt()) in_filename = self.get_filename('shp') out_filename = self.get_filename('sqlite') if os.path.exists(out_filename): os.unlink(out_filename) sh.ogr2ogr('-skipfailures', '-t_srs', 'epsg:3857', '-f', 'SQLite', '-dsco', 'SPATIALITE=YES', out_filename, in_filename) elif self.src_ext.endswith('gz'): archive = TarFile(self.cached_basename + self.src_ext) projection_found = False for name in archive.getnames(): xtn = name.split('.')[-1].lower() if xtn in {'shp', 'shx', 'dbf', 'prj' } and "__MACOSX" not in name: projection_found = projection_found or xtn == 'prj' with open(self.cached_basename + '.' + xtn, 'wb') as fout: with archive.open(name) as fin: chunk = fin.read(65536) while chunk: fout.write(chunk) chunk = fin.read(65536) if not projection_found: with open(self.cached_basename + '.prj', 'w') as f: srs = osr.SpatialReference() srs.ImportFromEPSG(4326) f.write(srs.ExportToWkt()) in_filename = self.get_filename('shp') out_filename = self.get_filename('sqlite') if os.path.exists(out_filename): os.unlink(out_filename) sh.ogr2ogr('-skipfailures', '-t_srs', 'epsg:3857', '-f', 'SQLite', '-dsco', 'SPATIALITE=YES', out_filename, in_filename) elif not self.src_ext.endswith('sqlite'): in_filename = self.get_filename(self.src_ext) out_filename = self.get_filename('sqlite') if os.path.exists(out_filename): os.unlink(out_filename) sh.ogr2ogr('-skipfailures', '-t_srs', 'epsg:3857', '-f', 'SQLite', '-dsco', 'SPATIALITE=YES', out_filename, in_filename) connection = self._connection() table, geometry_field, _, _, srid, _ = connection.execute( "select * from geometry_columns").fetchone( ) # grab the first layer with a geometry self._srid = srid if srid else 3857 dataframe = self.get_filename('dfx') if os.path.exists(dataframe): os.unlink(dataframe) c = connection.cursor() c.execute( "select AsText(Extent(w.{geom_field})) from {table} as w".format( geom_field=geometry_field, table=table)) try: xmin, ymin, xmax, ymax = GEOSGeometry(c.fetchone()[0]).extent except TypeError: xmin = ymin = xmax = ymax = 0.0 crs = osr.SpatialReference() crs.ImportFromEPSG(srid) self.resource.native_srs = crs.ExportToProj4() e4326 = osr.SpatialReference() e4326.ImportFromEPSG(4326) crx = osr.CoordinateTransformation(crs, e4326) x04326, y04326, _ = crx.TransformPoint(xmin, ymin) x14326, y14326, _ = crx.TransformPoint(xmax, ymax) self.resource.bounding_box = Polygon.from_bbox( (x04326, y04326, x14326, y14326)) self.resource.native_bounding_box = Polygon.from_bbox( (xmin, ymin, xmax, ymax)) self.resource.three_d = False self.resource.save()
from osgeo import gdal from osgeo import osr from osgeo import ogr import argparse from subprocess import call gdal.UseExceptions() to_srs = 3857 source = osr.SpatialReference() source.ImportFromEPSG(4326) target = osr.SpatialReference() target.ImportFromEPSG(3857) transform = osr.CoordinateTransformation(source, target) def parse_arguments(): parser = argparse.ArgumentParser( prog="georeference_tif", description="This program geo references a TIFF image to make a GeoTIFF") parser.add_argument("src_file", nargs=1, help="Your input TIFF image") parser.add_argument("out_file", nargs=1, help="The output GeoTIFF image") parser.add_argument( "position", nargs=2, help="The upper left geographic position (Latitude Longitude)") parser.add_argument("pixelsize", nargs=2, help="The pixel size in meters (Latitude Longitude)") return parser.parse_args() def main():
# Read geotransform matrix and calculate ground coordinates #geomatrix = indataset.GetGeoTransform() #X = geomatrix[0] + geomatrix[1] * pixel + geomatrix[2] * line #Y = geomatrix[3] + geomatrix[4] * pixel + geomatrix[5] * line # Shift to the center of the pixel #X += geomatrix[1] / 2.0 #Y += geomatrix[5] / 2.0 # Build Spatial Reference object based on coordinate system, fetched from the # opened dataset srs = osr.SpatialReference() srs.ImportFromWkt(indataset.GetProjection()) srsLatLong = srs.CloneGeogCS() ct = osr.CoordinateTransformation(srsLatLong, srs) (minX, minY, height) = ct.TransformPoint(minlong, minlat) (maxX, maxY, height) = ct.TransformPoint(maxlong, maxlat) lines = abs(maxX - minX) / res samples = abs(maxY - minY) / res if bitType == 8: imageSizeMB = lines * samples * bands elif bitType == 16: imageSizeMB = lines * samples * bands * 2 elif bitType == 32: imageSizeMB = lines * samples * bands * 4 else: print('bitType of %f not supported' % (bitType))
def transform_shapes_ogr(self, infile,outfile,out_prj): (outfilepath, outfilename) = os.path.split(outfile) (outfileshortname, extension) = os.path.splitext(outfilename) driver = ogr.GetDriverByName('ESRI Shapefile') indataset = driver.Open(infile, 0) if indataset is None: print('Could not open file') sys.exit(1) inlayer = indataset.GetLayer() inSpatialRef = inlayer.GetSpatialRef() outSpatialRef = osr.SpatialReference() prj_file = open(out_prj, 'r') prj_txt = prj_file.read() outSpatialRef.ImportFromESRI([prj_txt]) # MODIS SINUSOIDAL GRID # create Coordinate Transformation coordTransform = osr.CoordinateTransformation(inSpatialRef, outSpatialRef) # Create the output shapefile but check first if file exists if os.path.exists(outfile): print(outfile, ' file already exists. Will try to delete') driver.DeleteDataSource(outfile) #TODO: This apparently does not work! outdataset = driver.CreateDataSource(outfile) if outdataset is None: print('Could not create file: ', outfile) sys.exit(1) outlayer = outdataset.CreateLayer(outfileshortname, geom_type=ogr.wkbPolygon) #iluk has_subbasin = False if has_field(inlayer, 'Subbasin'): feature = inlayer.GetFeature(0) fieldDefn1 = feature.GetFieldDefnRef('Subbasin') outlayer.CreateField(fieldDefn1) has_subbasin = True else: outlayer.CreateField(ogr.FieldDefn('Subbasin', ogr.OFTInteger)) outlayer.CreateField(ogr.FieldDefn('surface', ogr.OFTReal)) outlayer.CreateField(ogr.FieldDefn('rootzone', ogr.OFTReal)) featureDefn = outlayer.GetLayerDefn() infeature = inlayer.GetNextFeature() index=1 while infeature: #get the input geometry geometry = infeature.GetGeometryRef() #reproject the geometry, each one has to be projected seperately geometry.Transform(coordTransform) #create a new output feature outfeature = ogr.Feature(featureDefn) #set the geometry and attribute outfeature.SetGeometry(geometry) if has_subbasin: outfeature.SetField('Subbasin', infeature.GetField('Subbasin')) else: outfeature.SetField('Subbasin', index) #add the feature to the output shapefile outlayer.CreateFeature(outfeature) #destroy the features and get the next input features outfeature.Destroy infeature.Destroy infeature = inlayer.GetNextFeature() index += 1 #close the shapefiles indataset.Destroy() outdataset.Destroy() #create the prj projection file outSpatialRef.MorphToESRI() file = open(outfilepath + '/'+ outfileshortname + '.prj', 'w') file.write(outSpatialRef.ExportToWkt()) file.close()