def coordinates( fn, latlong=True ): ''' take a raster file as input and return the centroid coords for each of the grid cells as a pair of numpy 2d arrays (longitude, latitude) ''' import rasterio import numpy as np from affine import Affine from pyproj import Proj, transform # Read raster with rasterio.open(fn) as r: T0 = r.affine # upper-left pixel corner affine transform p1 = Proj(r.crs) A = r.read( 1 ) # pixel values # All rows and columns cols, rows = np.meshgrid( np.arange( A.shape[1] ), np.arange( A.shape[0] ) ) # Get affine transform for pixel centres T1 = T0 * Affine.translation( 0.5, 0.5 ) # Function to convert pixel row/column index (from 0) to easting/northing at centre rc2en = lambda r, c: (c, r) * T1 # All eastings and northings (there is probably a faster way to do this) eastings, northings = np.vectorize(rc2en, otypes=[np.float, np.float])(rows, cols) if latlong != True: return eastings, northings elif latlong -- True: # Project all longitudes, latitudes to latlong longs, lats = transform(p1, p1.to_latlong(), eastings, northings) return longs, lats
def __init__(self, srs, bbox, width=None, height=None, format=None, resource_id=None): super(WmsQuery, self).__init__() self.query_type = 'WMS' self.srs = srs self.bbox = bbox self.width = width self.height = height self.format = format self.resource_id = resource_id if width is not None and height is not None: # calculate resolution... this should slow things down, yay... :-( p = Proj(init=srs.lower()) if not p.is_latlong(): min_lon, min_lat = p(bbox.min_x,bbox.min_y, inverse=True) max_lon, max_lat = p(bbox.max_x,bbox.max_y, inverse=True) else: min_lon, min_lat = bbox.min_x, bbox.min_y max_lon, max_lat = bbox.max_x, bbox.max_y g = Geod(ellps='clrk66') # Use Clarke 1966 ellipsoid. _,_,diagonal = g.inv(min_lon, min_lat, max_lon, max_lat) # distance calculated geodesic dist_x = sqrt(diagonal**2 / (1 + float(height)/float(width)) ) dist_y = dist_x * (float(height)/float(width)) self.x_res = dist_x / float(width) self.y_res = dist_y / float(height) else: self.x_res = None self.y_res = None
def coordinates( fn=None, meta=None, numpy_array=None, input_crs=None, to_latlong=False ): ''' take a raster file as input and return the centroid coords for each of the grid cells as a pair of numpy 2d arrays (longitude, latitude) User must give either: fn = path to the rasterio readable raster OR meta & numpy ndarray (usually obtained by rasterio.open(fn).read( 1 )) where: meta = a rasterio style metadata dictionary ( rasterio.open(fn).meta ) numpy_array = 2d numpy array representing a raster described by the meta input_crs = rasterio style proj4 dict, example: { 'init':'epsg:3338' } to_latlong = boolean. If True all coordinates will be returned as EPSG:4326 If False all coordinates will be returned in input_crs returns: meshgrid of longitudes and latitudes borrowed from here: https://gis.stackexchange.com/a/129857 ''' import rasterio import numpy as np from affine import Affine from pyproj import Proj, transform if fn: # Read raster with rasterio.open( fn ) as r: T0 = r.affine # upper-left pixel corner affine transform p1 = Proj( r.crs ) A = r.read( 1 ) # pixel values elif (meta is not None) & (numpy_array is not None): A = numpy_array if input_crs != None: p1 = Proj( input_crs ) T0 = meta[ 'affine' ] else: p1 = None T0 = meta[ 'affine' ] else: BaseException( 'check inputs' ) # All rows and columns cols, rows = np.meshgrid(np.arange(A.shape[1]), np.arange(A.shape[0])) # Get affine transform for pixel centres T1 = T0 * Affine.translation( 0.5, 0.5 ) # Function to convert pixel row/column index (from 0) to easting/northing at centre rc2en = lambda r, c: ( c, r ) * T1 # All eastings and northings -- this is much faster than np.apply_along_axis eastings, northings = np.vectorize(rc2en, otypes=[np.float, np.float])(rows, cols) if to_latlong == False: return eastings, northings elif (to_latlong == True) & (input_crs != None): # Project all longitudes, latitudes longs, lats = transform(p1, p1.to_latlong(), eastings, northings) return longs, lats else: BaseException( 'cant reproject to latlong without an input_crs' )
def extentToLatLon(extent, proj): if proj == '' or proj is None: return None p1 = Proj(proj, preserve_units=True) if p1.is_latlong(): return extent x1,y1 = p1(extent[0], extent[1], inverse=True) x2,y2 = p1(extent[2], extent[3], inverse=True) return x1, y1, x2, y2
def coordinates( fn=None, meta=None, numpy_array=None, input_crs=None, to_latlong=False ): ''' take a raster file as input and return the centroid coords for each of the grid cells as a pair of numpy 2d arrays (longitude, latitude) ''' import rasterio import numpy as np from affine import Affine from pyproj import Proj, transform if fn: # Read raster with rasterio.open( fn ) as r: T0 = r.affine # upper-left pixel corner affine transform p1 = Proj( r.crs ) A = r.read_band( 1 ) # pixel values elif (meta is not None) & (numpy_array is not None): A = numpy_array if input_crs != None: p1 = Proj( input_crs ) T0 = meta[ 'affine' ] else: p1 = None T0 = meta[ 'affine' ] else: BaseException( 'check inputs' ) # All rows and columns cols, rows = np.meshgrid(np.arange(A.shape[1]), np.arange(A.shape[0])) # Get affine transform for pixel centres T1 = T0 * Affine.translation( 0.5, 0.5 ) # Function to convert pixel row/column index (from 0) to easting/northing at centre rc2en = lambda r, c: ( c, r ) * T1 # All eastings and northings (there is probably a faster way to do this) eastings, northings = np.vectorize(rc2en, otypes=[np.float, np.float])(rows, cols) if to_latlong == False: return eastings, northings elif (to_latlong == True) & (input_crs != None): # Project all longitudes, latitudes longs, lats = transform(p1, p1.to_latlong(), eastings, northings) return longs, lats else: BaseException( 'cant reproject to latlong without an input_crs' )
def mosaic_and_clip(raster_tiles, xmin, ymin, xmax, ymax, output_path): from pyproj import Proj import rasterio import subprocess print 'Mosaic and clip to bounding box extents' output_vrt = os.path.splitext(output_path)[0] + '.vrt' print subprocess.check_output(['gdalbuildvrt', '-overwrite', output_vrt] + raster_tiles) # check crs with rasterio.drivers(): with rasterio.open(output_vrt) as src: p = Proj(src.crs) if not p.is_latlong(): [xmax, xmin],[ymax, ymin] = p([xmax, xmin],[ymax,ymin]) print subprocess.check_output(['gdalwarp', '-overwrite', '-te', repr(xmin), repr(ymin), repr(xmax), repr(ymax), output_vrt, output_path]) print 'Output raster saved at %s', output_path
def __init__(self, beating_angle=45, utm_zone=30): """ position_ll : Position as a LatLon object heading : Compass heading beating_angle : Closest absolute angle relative to the wind that we can sail utm_zone : Zone number of the UTM system to use. Southampton is in zone 30, Portugal in zone 29. http://www.dmap.co.uk/utmworld.htm Distance calculations will be less accurate the further from the specified zone you are. """ self.projection = Proj(proj='utm', zone=utm_zone, ellps='WGS84') self.position_ll = ll = LatLon(50.8, 1.02) x, y = self.latlon_to_utm(ll.lat.decimal_degree, ll.lon.decimal_degree) self.position_xy = Point(x, y) self.heading = 0. self.wind_direction = 0. self.beating_angle = beating_angle
def test_initialize_proj_crs_no_plus(): proj = Proj("proj=lonlat") assert proj.crs.srs == "proj=lonlat type=crs"
def regression_subset(predictions, train, test, method): mean_error = [] if (method == 1): machine_learn = KNeighborsRegressor(n_neighbors=5, weights='distance') elif (method == 2): machine_learn = MLPRegressor(random_state=0) #for each building for i in range(3): new_train = train.loc[ train['BUILDINGID'] == i] #select for training only buildings with that label (0,1, or 2) indexes = [x for x in range(len(predictions)) if predictions[x] == i ] #get the position of the samples that have building == i if (indexes): #if list is not empty #training, samples with building == i X_train = new_train.ix[:, 0:519] Y_train = new_train[['LONGITUDE', 'LATITUDE']] machine_learn.fit(X_train, Y_train) #testing samples with prediction building == i new_test = test.iloc[indexes, :] X_test = new_test.ix[:, 0:519] Y_test = new_test[['LONGITUDE', 'LATITUDE']] #Turn into list predicts_lon_lat = machine_learn.predict(X_test).tolist() Y_test = Y_test.values.tolist() distance = [] for j in range(len(predicts_lon_lat)): #change the latitude and longitude unit myProj = Proj( "+proj=utm +zone=23K, +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs" ) lon_pred, lat_pred = myProj(predicts_lon_lat[j][0], predicts_lon_lat[j][1], inverse=True) lon_Y, lat_Y = myProj(Y_test[j][0], Y_test[j][1], inverse=True) #join in a unique list Y = [] Y.append(lon_Y) Y.append(lat_Y) predict = [] predict.append(lon_pred) predict.append(lat_pred) #The distance between the two latitudes is the error distance.append(vincenty(Y, predict).meters) #If you want to use haversine distance, uncomment the line below #print haversine(lon_Y, lat_Y, lon_pred, lat_pred) mean_error.append(np.mean(distance)) #print(np.mean(distance)) return np.mean(mean_error)
def _query(self, data: List[SeismicData], **kwargs) -> bool: """ This is the method that all models override. It handles retrieving the queried models' properties and adjusting the SeismicData structures as need be. Args: points (:obj:`list` of :obj:`SeismicData`): List of SeismicData objects containing the points queried. These are to be adjusted if needed. Returns: True on success, false if there is an error. """ spacing = 500 if "params" in kwargs: if is_number(kwargs["params"]): spacing = kwargs["params"] proj_in = Proj(UCVM_DEFAULT_PROJECTION) for datum in data: proj_out = Proj( "+proj=utm +datum=WGS84 +zone=%d" % (get_utm_zone_for_lon(datum.converted_point.x_value))) utm_coords = transform(proj_in, proj_out, datum.converted_point.x_value, datum.converted_point.y_value) # Get the four corners. corners_utm_e = [ utm_coords[0] - spacing / 2, utm_coords[0] - spacing / 2, utm_coords[0] + spacing / 2, utm_coords[0] + spacing / 2 ] corners_utm_n = [ utm_coords[1] - spacing / 2, utm_coords[1] + spacing / 2, utm_coords[1] - spacing / 2, utm_coords[1] + spacing / 2 ] lat_lon_corners = transform(proj_out, proj_in, corners_utm_e, corners_utm_n) sd_array = [ SeismicData( Point(lat_lon_corners[0][0], lat_lon_corners[1][0], datum.original_point.z_value, datum.original_point.depth_elev)), SeismicData( Point(lat_lon_corners[0][1], lat_lon_corners[1][1], datum.original_point.z_value, datum.original_point.depth_elev)), SeismicData( Point(lat_lon_corners[0][2], lat_lon_corners[1][2], datum.original_point.z_value, datum.original_point.depth_elev)), SeismicData( Point(lat_lon_corners[0][3], lat_lon_corners[1][3], datum.original_point.z_value, datum.original_point.depth_elev)), SeismicData( Point(lat_lon_corners[0][0], lat_lon_corners[1][0], datum.original_point.z_value - spacing / 2, datum.original_point.depth_elev)), SeismicData( Point(lat_lon_corners[0][1], lat_lon_corners[1][1], datum.original_point.z_value - spacing / 2, datum.original_point.depth_elev)), SeismicData( Point(lat_lon_corners[0][2], lat_lon_corners[1][2], datum.original_point.z_value - spacing / 2, datum.original_point.depth_elev)), SeismicData( Point(lat_lon_corners[0][3], lat_lon_corners[1][3], datum.original_point.z_value - spacing / 2, datum.original_point.depth_elev)), SeismicData( Point(lat_lon_corners[0][0], lat_lon_corners[1][0], datum.original_point.z_value + spacing / 2, datum.original_point.depth_elev)), SeismicData( Point(lat_lon_corners[0][1], lat_lon_corners[1][1], datum.original_point.z_value + spacing / 2, datum.original_point.depth_elev)), SeismicData( Point(lat_lon_corners[0][2], lat_lon_corners[1][2], datum.original_point.z_value + spacing / 2, datum.original_point.depth_elev)), SeismicData( Point(lat_lon_corners[0][3], lat_lon_corners[1][3], datum.original_point.z_value + spacing / 2, datum.original_point.depth_elev)), ] # Now query for the material properties. UCVM.query(sd_array, datum.model_string, ["velocity"]) def contains_num_in_range( num_list: List(int), start_index: int, stop_index: int): for item in num_list: if start_index <= item <= stop_index: return True return False model_string_differences = [] for i in range(len(sd_array)): if sd_array[i].model_string != datum.model_string: model_string_differences.append(i) if sd_array[4].is_property_type_set("velocity") and \ sd_array[5].is_property_type_set("velocity") and \ sd_array[6].is_property_type_set("velocity") and \ sd_array[7].is_property_type_set("velocity") and \ sd_array[8].is_property_type_set("velocity") and \ sd_array[9].is_property_type_set("velocity") and \ sd_array[10].is_property_type_set("velocity") and \ sd_array[11].is_property_type_set("velocity") and \ contains_num_in_range(model_string_differences, 4, 11): datum.set_velocity_data( self._interpolate_properties(sd_array[4:11])) elif sd_array[0].is_property_type_set("velocity") and \ sd_array[1].is_property_type_set("velocity") and \ sd_array[2].is_property_type_set("velocity") and \ sd_array[3].is_property_type_set("velocity") and \ contains_num_in_range(model_string_differences, 0, 3): datum.set_velocity_data( self._interpolate_properties(sd_array[0:3])) return True
def set_projection(self, projstring): try: return Proj(projstring) except RuntimeError: log.ODM_EXCEPTION( 'Could not set projection. Please use a proj4 string')
def bearing_deg(*args): # lat1, lon1, lat2, lon2 args = map(deg2rad, args) return rad2deg(bearing(*args)) class LinearInterpolation: def __init__(self, x1, y1, x2, y2): self.m = (float(y2) - float(y1)) / (float(x2) - float(x1)) self.n = float(y1) - self.m * float(x1) def calc(self, x): return self.m * float(x) + self.n _gps = Proj(proj = 'latlong', datum = 'WGS84') _aeqd = Proj(proj = 'aeqd') def gps_add_meters((lat, lon), (dx, dy)): y, x = transform(_gps, _aeqd, lat, lon) y += dy; x += dx return transform(_aeqd, _gps, y, x) def gps_meters_offset((lat1, lon1), (lat2, lon2)): y1, x1 = transform(_gps, _aeqd, lat1, lon1) y2, x2 = transform(_gps, _aeqd, lat2, lon2) return x2 - x1, y2 - y1
return uniq, seen fnconf = 'config_sand_clay_off_grid.cfg' (Shp_name, Fasso, Fse, lonmin, lonmax, latmin, latmax, projection, ellipse, lat0, lon0, x0, y0, k0, fnsand, fnclay) = rc.readconf(fnconf) #read the shapefile sf = shp.Reader(Shp_name) fields, records, shps, Association_1, Association_2, asso_unit = read_shapefile( sf) indices = np.arange(0, 50) #generates a function to convert the prjected data (needs a function to read it in the Readme.txt) pnyc = Proj(proj=projection, ellps=ellipse, lat_0=lat0, lon_0=lon0, x_0=x0, y_0=y0, k_0=k0) #+a Semimajor radius of the ellipsoid axis #+axis Axis orientation #+b Semiminor radius of the ellipsoid axis #+ellps Ellipsoid name (see proj -le) #+k Scaling factor (deprecated) #+k_0 Scaling factor #+lat_0 Latitude of origin #+lon_0 Central meridian #+lon_wrap Center longitude to use for wrapping (see below) #+over Allow longitude output outside -180 to 180 range, disables wrapping (see below) #+pm Alternate prime meridian (typically a city name, see below) #+proj Projection name (see proj -l)
def find_gridcell( lon, lat, zlonc, zlatc, orig_proj=Proj(init="epsg:4326"), isregular=False, is_unstructured=False, discont=180, ): """Finds the grid cell corresponding to a coordinate Args: lon (np.array): longitude of the point to find lat (np.array): latitude of the point to find zlonc (np.array): longitudes of the corners of the grid zlatc (np.array): latitudes of the corners of the grid orig_proj (projection): the projection for reporting coordinates. Default is WSG84 isregular (Boolean): if True, simplified operations are computed. Default is False Returns: i, j the grid cell ID Notes: For very regular grids, this script could be made more effileient and shorter, but the objective is to be able to deal with any domain """ # If regular domain make simplified operations if isregular and not is_unstructured: try: xlon = 1.0 / np.unwrap(zlonc[0, np.newaxis] - lon[:, np.newaxis], discont=discont) xlat = 1.0 / (lat[:, np.newaxis] - zlatc[:, 0]) return xlat.argmin(axis=1), xlon.argmin(axis=1) # If only one (lon, lat) tuple was provided except TypeError as e: info(e) xlon = 1.0 / np.unwrap(zlonc[0] - lon, discont=discont) xlat = 1.0 / (lat - zlatc[:, 0]) return xlat.argmin(), xlon.argmin() # Grid shape nlat, nlon = zlonc.shape # Define measurement point geometry in local coordinates point = ogr.Geometry(ogr.wkbPoint) point.AddPoint(0, 0) # Define local projection target_proj = Proj("+proj=laea +lat_0={} +lon_0={} +x_0=0 +y_0=0 " "+ellps=WGS84 +units=m +no_defs ".format(lat, lon)) zxc, zyc = transform(orig_proj, target_proj, (zlonc + 180) % 360 - 180, zlatc) dist = zxc**2 + zyc**2 imin, jmin = np.unravel_index(dist.argmin(), dist.shape) imin = imin % nlat jmin = jmin % nlon for i in range(imin - 3, imin + 2): for j in range(jmin - 3, jmin + 2): # Looping if the minimum is close to the domain side i = max(min(i, nlat - 2), 0) j = max(min(j, nlon - 2), 0) # Create ring ring = ogr.Geometry(ogr.wkbLinearRing) _ = ring.AddPoint(zxc[i, j], zyc[i, j]) _ = ring.AddPoint(zxc[i + 1, j], zyc[i + 1, j]) _ = ring.AddPoint(zxc[i + 1, j + 1], zyc[i + 1, j + 1]) _ = ring.AddPoint(zxc[i, j + 1], zyc[i, j + 1]) _ = ring.AddPoint(zxc[i, j], zyc[i, j]) # Create polygon poly = ogr.Geometry(ogr.wkbPolygon) _ = poly.AddGeometry(ring) # Buffering the polygon to include points on the edge buffer = 1.0 poly = poly.Buffer(buffer) if point.Within(poly): return i, j # If no grid cell was fund, raise exception # # raise IndexError( # "No index was found for measurement at {}, {}".format(lon, lat)) # TODO: Decide whether we raise exception # when the observation is outside the domain return np.nan, np.nan
AUTHORITY["EPSG","9122"]], AUTHORITY["EPSG","4053"]], UNIT["metre",1, AUTHORITY["EPSG","9001"]], PROJECTION["Lambert_Azimuthal_Equal_Area"], PARAMETER["latitude_of_center",-90], PARAMETER["longitude_of_center",0], PARAMETER["false_easting",0], PARAMETER["false_northing",0], AUTHORITY["EPSG","3409"], AXIS["X",UNKNOWN], AXIS["Y",UNKNOWN]]''' #NSIDC South Pole EASE-Grid spat_res = 25000 #pixel spatial resolution in meters inProj = Proj(init='epsg:4326') # Load GDAL GeoTIFF dataset driver for dataset format drv = gdal.GetDriverByName("GTiff") #looping over 4 hdf data layers within each data file for i in range(len(products)): #grab data from file_dict (list of data layer arrays) indata = file_dict[products[i]] #output file path using names in file_dict outfile = outfolder + products[i] + '.tif' #Checks if North or South NSIDC Ease Grid pPojection applies #set upper left lon and lat extent dimensions for polar data
# See the GNU Affero Public License for more details. # # You should have received a copy of the GNU Affero Public License # along with this program. If not, see http://www.gnu.org/licenses. # # http://numenta.org/licenses/ # ---------------------------------------------------------------------- import math import numpy from pyproj import Proj, transform from nupic.encoders.coordinate import CoordinateEncoder # From http://spatialreference.org/ref/epsg/popular-visualisation-crs-mercator/ PROJ = Proj(init="epsg:3785") # Spherical Mercator PROJ_RANGE = (20037508.3428, 19971868.8804) # in meters # See http://gis.stackexchange.com/a/73829/41082 geocentric = Proj('+proj=geocent +datum=WGS84 +units=m +no_defs') class GeospatialCoordinateEncoder(CoordinateEncoder): """ Given a GPS coordinate and a speed reading, the Geospatial Coordinate Encoder returns an SDR representation of that position. """ def __init__(self, scale, timestep, w=21, n=1000, name=None, verbosity=0): """ See `nupic.encoders.base.Encoder` for more information.
appartments['Y'] = np.zeros(n) appartments['Park'] = np.zeros(n) appartments['Crime'] = np.zeros(n) appartments['School'] = np.zeros(n) appartments['Subway'] = np.zeros(n) appartments['Poi'] = np.zeros(n) appartments['Bar'] = np.zeros(n) appartments['Gym'] = np.zeros(n) appartments['Library'] = np.zeros(n) appartments['Restaurant'] = np.zeros(n) appartments['Supermarket'] = np.zeros(n) # Project to web mercator from pyproj import Proj myProj = Proj(init="epsg:3857") for i in range(n): lat = appartments['Lat'][i] lon = appartments['Lon'][i] x, y = myProj(lon, lat) appartments.loc[i, 'X'] = x appartments.loc[i, 'Y'] = y #%% # remove appartments that are not in our study area appartments = appartments.drop( appartments[(appartments['X'] < -8865296) | (appartments['X'] > -8807096)].index) appartments = appartments.drop(appartments[(appartments['Y'] < 5400751) | (appartments['Y'] > 5443102)].index)
def transformPoint(x, y): inProj = Proj(init='epsg:25832') outProj = Proj(init='epsg:4326') x2, y2 = transform(inProj, outProj, x, y) return x2, y2
def __init__(self, **kwargs): # Default Map Center Location and Zoom Level latlon = [50.779305, 6.078914] zoom = 8 # Get Map Center Location if provided if 'location' in kwargs.keys(): kwargs['center'] = kwargs.get('location', latlon) kwargs.pop('location') elif 'center' in kwargs.keys(): kwargs['center'] = kwargs.get('center', latlon) else: kwargs['center'] = latlon # Check if a crs is provided if 'location' in kwargs.keys() or 'center' in kwargs.keys(): if 'crs' not in kwargs.keys(): # If no crs is provided, check if coordinates are in lat/lon range # If coordinates are out of range return a Value error to either fix the coordinates # or provide correct crs if kwargs['center'][0] > 90 or kwargs['center'][0] < -90: if kwargs['center'][1] > 180 or kwargs['center'][1] < -180: raise ValueError( 'Lat and Lon Coordinates invalid or provide valid CRS' ) else: raise ValueError( 'Lat Coordinates invalid or provide valid CRS') elif 90 > kwargs['center'][0] > -90: if kwargs['center'][1] > 180 or kwargs['center'][1] < -180: raise ValueError( 'Lon Coordinates invalid or provide valid CRS') else: pass # If a crs is provided, convert provided center cooridantes to WGS84 for accurate location of the center elif 'crs' in kwargs.keys(): proj_custom = Proj(init=kwargs.get('crs', None)) proj_deafult = Proj(init='epsg:4326') kwargs['center'][1], kwargs['center'][0] = transform( proj_custom, proj_deafult, kwargs['center'][0], kwargs['center'][1]) kwargs['crs'] = self.crs # Check if zoom argument is provided if 'zoom_start' in kwargs.keys(): kwargs['zoom'] = kwargs.get('zoom_start', zoom) kwargs.pop('zoom_start') elif 'zoom' in kwargs.keys(): kwargs['zoom'] = kwargs.get('zoom', zoom) # Inherits the ipyleaflet Map class super().__init__(**kwargs) # Set zooming, dragging and layout height self.scroll_wheel_zoom = True self.dragging = True self.layout.height = '550px' # Clear controls self.clear_controls() # Set new controls self.add_control(ZoomControl(position='topright')) self.add_control(ScaleControl(position='bottomleft', imperial=False)) self.add_control(LayersControl(position='topleft')) self.add_control(FullScreenControl()) self.add_control( MeasureControl(position='bottomright', active_color='orange', primary_length_unit='kilometers')) # Adding Map Attributes self.center = kwargs['center']
def test_proj_equals(): assert Proj(4326) == Proj("epsg:4326") assert Proj(4326) != Proj("epsg:3857") assert Proj(4326) == Proj(Proj("epsg:4326").crs.to_proj4())
def initial(self): """ initial part of the misc module """ if option['gridSizeUserDefined']: # <lfoption name="gridSizeUserDefined" choice="1" default="0"> # If option gridsizeUserDefined is activated, users can specify grid size properties # in separate maps. This is useful whenever this information cannot be derived from # the location attributes of the base maps (e.g. lat/lon systems or non-equal-area # projections) # Limitation: always assumes square grid cells (not rectangles!). Size of grid cells # may vary across map though self.var.PixelLengthPcr = loadmap('PixelLengthUser', pcr=True) self.var.PixelLength = compressArray(self.var.PixelLengthPcr) # Length of pixel [m] # Area of pixel [m2] self.var.PixelAreaPcr = loadmap('PixelAreaUser', pcr=True) self.var.PixelArea = compressArray(self.var.PixelAreaPcr) else: # Default behaviour: grid size is derived from location attributes of # base maps. Requirements: # - Maps are in some equal-area projection # - Length units meters # - All grid cells have the same size # Length of pixel [m] #self.var.PixelLength = celllength() self.var.PixelLengthPcr = celllength() self.var.PixelLength = maskmapAttr['cell'] # Area of pixel [m2] self.var.PixelAreaPcr = self.var.PixelLength**2 self.var.PixelArea = np.empty(maskinfo['mapC']) self.var.PixelArea.fill(self.var.PixelLength**2) # self.var.PixelArea = spatial(self.var.PixelArea) # Convert to spatial expresion (otherwise this variable cannnot be # used in areatotal function) # ----------------------------------------------------------------- # Miscellaneous repeatedly used expressions (as suggested by GF) self.var.InvPixelLength = 1.0 / self.var.PixelLength # Inverse of pixel size [1/m] self.var.DtSec = loadmap('DtSec') self.var.DtDay = self.var.DtSec / 86400 # Time step, expressed as fraction of day (used to convert # rate variables that are expressed as a quantity per day to # into an amount per time step) self.var.InvDtSec = 1 / self.var.DtSec # Inverse of time step [1/s] self.var.InvDtDay = 1 / self.var.DtDay # Inverse of time step [1/d] self.var.DtSecChannel = loadmap('DtSecChannel') # Sub time step used for kinematic wave channel routing [seconds] # within the model,the smallest out of DtSecChannel and DtSec is used self.var.MMtoM = 0.001 # Multiplier to convert wate depths in mm to meters self.var.MtoMM = 1000 # Multiplier to convert wate depths in meters to mm self.var.MMtoM3 = 0.001 * self.var.PixelArea # self.var.MMtoM3=0.001*float(celllength())**2 # Multiplier to convert water depths in mm to cubic # metres self.var.M3toMM = 1 / self.var.MMtoM3 # Multiplier to convert from cubic metres to mm water slice self.var.GwLoss = loadmap('GwLoss') self.var.GwPerc = np.maximum(loadmap('GwPercValue'), self.var.GwLoss) # new Gwloss PB 12.11.2009 # if GWloss > GwPercValue -> GwPerc = GwLoss self.var.GwPercStep = self.var.GwPerc * self.var.DtDay # Percolation from upper to lower groundwater zone, expressed as # amount per time step self.var.GwLossStep = self.var.GwLoss * self.var.DtDay # change similar to GwPercStep # ************************************************************ # ***** Some additional stuff # ************************************************************ # date of the first possible model run # computation of model steps is referred to CalendarStartDay self.var.CalendarDayStart = Calendar(binding['CalendarDayStart']) try: # number of time step or date of the state map to be used to initialize model run timestepInit.append(binding["timestepInit"]) except: pass self.var.PrScaling = loadmap('PrScaling') self.var.CalEvaporation = loadmap('CalEvaporation') self.var.Precipitation = None self.var.Tavg = None self.var.ETRef = None self.var.ESRef = None self.var.EWRef = None # setting meteo data to none - is this necessary? self.var.DayCounter = 0.0 self.var.MonthETpot = globals.inZero self.var.MonthETact = globals.inZero self.var.MonthWDemand = globals.inZero self.var.MonthWUse = globals.inZero self.var.MonthWDemand = globals.inZero self.var.MonthDis = globals.inZero self.var.MonthInternalFlow = globals.inZero self.var.TotalInternalFlowM3 = globals.inZero self.var.PerMonthInternalFlowM3 = globals.inZero # total freshwater generated in the sub-area (m3), basically local P-ET-Storage self.var.TotalExternalInflowM3 = globals.inZero self.var.PerMonthExternalInflowM3 = globals.inZero # Total channel inflow (m3) from inlet points self.var.PerMonthWaterDemandM3 = globals.inZero self.var.PerMonthWaterUseM3 = globals.inZero self.var.FlagDemandBiggerUse = scalar(0.0) self.var.TotWEI = scalar(0.0) self.var.TotlWEI = scalar(0.0) self.var.TotCount = scalar(0.0) self.var.SumETpot = globals.inZero self.var.SumETpotact = globals.inZero # Read the latitude (radians) from the precipitation forcing NetCDF file with xr.open_dataset(binding["PrecipitationMaps"] + ".nc") as nc: if all([co in nc.dims for co in ("x", "y")]): try: proj_var = [ v for v in nc.data_vars.keys() if 'proj4_params' in nc[v].attrs.keys() ][0] # look for the projection variable except IndexError: raise Exception( "If using projected coordinates (x, y), a variable with the 'proj4_params' attribute must be included in the precipitation file!" ) projection = Proj( nc[proj_var].attrs['proj4_params'] ) # projection object obtained from the PROJ4 string _, lat_deg = projection(*coordinatesLand( nc.x.values, nc.y.values), inverse=True) # latitude (degrees) else: _, lat_deg = coordinatesLand( nc.lon.values, nc.lat.values) # latitude (degrees) self.var.lat_rad = np.radians(lat_deg) # latitude (radians)
def test_latlong_typeerror(self): p = Proj('+proj=stere +lon_0=-39 +lat_0=90 +lat_ts=71.0 +ellps=WGS84') self.assertTrue(isinstance(p, Proj)) # if not patched this line raises a "TypeError: p2 must be a Proj class" lon, lat = transform(p, p.to_latlong(), 200000, 400000)
def get_lon_lat(): wgs84 = Proj(init='epsg:4326') mercator = Proj(init='epsg:3857') lon, lat = transform(mercator, wgs84, center_x, center_y) return lon, lat
def main(): import argparse parser = argparse.ArgumentParser( description= "Convert AHI Geos NetCDF files to mercator geotiffs at the same resolution" ) parser.add_argument( "--merc-ext", default=".merc.tif", help= "Extension for new mercator files (replace '.tif' with '.merc.tif' by default)" ) parser.add_argument( "--input-pattern", default="????/*.nc", help="Input pattern used search for NetCDF files in 'input_dir'") parser.add_argument( '-v', '--verbose', dest='verbosity', action="count", default=0, help= 'each occurrence increases verbosity 1 level through ERROR-WARNING-INFO-DEBUG (default INFO)' ) # http://www.gdal.org/frmt_gtiff.html parser.add_argument( '--compress', default=None, help="Type of compression for geotiffs (passed to GDAL GeoTIFF Driver)" ) parser.add_argument( '--predictor', default=None, type=int, help="Set predictor for geotiff compression (LZW or DEFLATE)") parser.add_argument('--tiled', action='store_true', help="Create tiled geotiffs") parser.add_argument('--blockxsize', default=None, type=int, help="Set tile block X size") parser.add_argument('--blockysize', default=None, type=int, help="Set tile block Y size") parser.add_argument( '--extents', default=[np.nan, np.nan, np.nan, np.nan], nargs=4, type=float, help= "Set mercator bounds in lat/lon space (lon_min lat_min lon_max lat_max)" ) parser.add_argument( '--nodata', default=None, type=float, help="Set the nodata value for the geotiffs that are created") parser.add_argument( "input_dir", help="Input directory to search for the 'input_pattern' specified") parser.add_argument( "output_dir", help= "Output directory to place new mercator files (input_pattern structure is reflected in output dir)" ) parser.add_argument("gdalwarp_args", nargs="*", help="arguments that are passed directly to gdalwarp") args = parser.parse_args() levels = [logging.ERROR, logging.WARN, logging.INFO, logging.DEBUG] logging.basicConfig(level=levels[min(3, args.verbosity)]) idir = args.input_dir odir = args.output_dir ipat = args.input_pattern for nc_file in glob(os.path.join(idir, ipat)): if not os.path.isfile(nc_file): LOG.error("Not a file '%s'" % (nc_file, )) continue # Come up with an intermediate geotiff name geos_file = nc_file.replace(idir, odir, 1).replace(".nc", ".tif") opath = os.path.dirname(geos_file) if not os.path.exists(opath): LOG.info("Creating output directory: %s", opath) os.makedirs(opath, exist_ok=True) # Come up with an output mercator filename merc_file = geos_file.replace(".tif", args.merc_ext) if os.path.isfile(merc_file): LOG.warning( "Output file already exists, will delete to start over: %s" % (merc_file, )) try: os.remove(merc_file) except Exception: LOG.error("Could not remove previous file: %s" % (merc_file, )) continue try: src_info = ahi_image_info(nc_file) if not os.path.exists(geos_file): src_data = ahi_image_data(nc_file) # print("### Resource Data: Min (%f) | Max (%f)" % (src_data.min(), src_data.max())) create_ahi_geotiff(src_info, src_data, geos_file, compress=args.compress, predictor=args.predictor, tiled=args.tiled, blockxsize=args.blockxsize, blockysize=args.blockysize, nodata=args.nodata) else: LOG.debug( "GEOS Projection GeoTIFF already exists, won't recreate..." ) lon_west, lon_east = src_info["lon_extents"] except RuntimeError: LOG.error("Could not create geotiff for '%s'" % (nc_file, )) LOG.debug("Exception Information: ", exc_info=True) continue # Get information about the geotiff gtiff = gdal.Open(geos_file) srs = osr.SpatialReference() srs.ImportFromWkt(gtiff.GetProjection()) proj = srs.ExportToProj4() if "+proj=geos" not in proj: LOG.warning("Tried to process non-geos geotiff: %s" % (geos_file, )) ox, cw, _, oy, _, ch = gtiff.GetGeoTransform() # Run gdalwarp LOG.info("Running gdalwarp on '%s' to create '%s'", geos_file, merc_file) proj = DEFAULT_PROJ_STR proj = proj + " +over" if lon_east >= 180 else proj # Include the '+over' parameter so longitudes are wrapper around the antimeridian src_proj = Proj(proj) # use image bounds if np.isnan(args.extents[0]): args.extents[0] = lon_west if np.isnan(args.extents[1]): args.extents[1] = -80 if np.isnan(args.extents[2]): args.extents[2] = lon_east if np.isnan(args.extents[3]): args.extents[3] = 80 x_min, y_min = src_proj(args.extents[0], args.extents[1]) x_max, y_max = src_proj(args.extents[2], args.extents[3]) x_extent = (x_min, x_max) y_extent = (y_min, y_max) LOG.debug("Using extents (%f : %f : %f : %f)", x_extent[0], y_extent[0], x_extent[1], y_extent[1]) gdalwarp_args = args.gdalwarp_args + [ #"-multi", "-t_srs", proj, "-tr", str(cw), str(ch), "-te", "{:0.03f}".format(x_extent[0]), "{:0.03f}".format(y_extent[0]), "{:0.03f}".format(x_extent[1]), "{:0.03f}".format(y_extent[1]), ] if args.nodata is not None: gdalwarp_args.extend([ "-srcnodata", str(args.nodata), "-dstnodata", str(args.nodata), ]) if args.compress is not None: gdalwarp_args.extend(["-co", "COMPRESS=%s" % (args.compress, )]) if args.predictor is not None: gdalwarp_args.extend( ["-co", "PREDICTOR=%d" % (args.predictor, )]) if args.tiled: gdalwarp_args.extend(["-co", "TILED=YES"]) if args.blockxsize is not None: gdalwarp_args.extend( ["-co", "BLOCKXSIZE=%d" % (args.blockxsize, )]) if args.blockysize is not None: gdalwarp_args.extend( ["-co", "BLOCKYSIZE=%d" % (args.blockysize, )]) run_gdalwarp(geos_file, merc_file, *gdalwarp_args)
iEPSG = options.iEPSG oEPSG = options.oEPSG pow_beam_list = options.pow_beam_list cFrac = options.cFrac random.seed(options.seed) miss_len = options.miss_len outNamen = options.outNamen skip_track_list = options.skip_track_list usePhen = options.usePhen # mission 0 time in days t0 = 11 * 30.4375 + 1 # tanslate bounds from ALS to orbit projection epsg = "epsg:" + str(iEPSG) inProj = Proj(init=epsg) epsg = "epsg:" + str(oEPSG) outProj = Proj(init=epsg) bX, bY = transform(outProj, inProj, bounds[0:2], bounds[2:4]) # resolution in metres #sRes=transform(outProj,inProj,0,60)[1] sRes = 60.0 #(60/6371007.181)*180/pi # read MODIS data_lc, data_vcf, data_leafon, data_leafoff, xOrigin, yOrigin, pixelWidth, pixelHeight, cols, rows = readMODIS( modNamen) # open output fOut = open(outNamen, 'w')
def render_tif(filename_pattern, output_directory, renderer_file, save, renderer_type, colormap, colorspace, palette, scale, id_variable, lh, legend_breaks, legend_ticks, src_crs, dst_crs, res, resampling, anchors): """ Render single-band GeoTIFF files to images. colormap is ignored if renderer_file is provided """ filenames = glob.glob(filename_pattern) if not filenames: raise click.BadParameter('No files found matching that pattern', param='filename_pattern', param_hint='FILENAME_PATTERN') if not os.path.exists(output_directory): os.makedirs(output_directory) if renderer_file is not None and not save: if not os.path.exists(renderer_file): raise click.BadParameter('does not exist', param='renderer_file', param_hint='renderer_file') # see https://bitbucket.org/databasin/ncdjango/wiki/Home for format renderer_dict = json.loads(open(renderer_file).read()) # if renderer_dict['type'] == 'stretched': # colors = ','.join([str(c[0]) for c in renderer_dict['colors']]) # if 'min' in colors or 'max' in colors or 'mean' in colors: # statistics = collect_statistics(filenames, (variable,))[variable] # for entry in renderer_dict['colors']: # if isinstance(entry[0], basestring): # if entry[0] in ('min', 'max', 'mean'): # entry[0] = statistics[entry[0]] # elif '*' in entry[0]: # rel_value, statistic = entry[0].split('*') # entry[0] = float(rel_value) * statistics[statistic] renderer = renderer_from_dict(renderer_dict) else: if renderer_type == 'stretched': # if palette is not None: # renderer = _palette_to_stretched_renderer(palette, 'min,max', filenames, variable) # # else: renderer = _colormap_to_stretched_renderer(colormap, colorspace, filenames) elif renderer_type == 'unique': renderer = UniqueValuesRenderer(_parse_colormap(colormap), colorspace) else: raise NotImplementedError('other renderers not yet built') # if save: # if not renderer_file: # raise click.BadParameter('must be provided to save', param='renderer_file', param_hint='renderer_file') # # if os.path.exists(renderer_file): # with open(renderer_file, 'r+') as output_file: # data = json.loads(output_file.read()) # output_file.seek(0) # output_file.truncate() # data[variable] = renderer.serialize() # output_file.write(json.dumps(data, indent=4)) # else: # with open(renderer_file, 'w') as output_file: # output_file.write(json.dumps({variable: renderer.serialize()})) if renderer_type == 'streteched': if legend_ticks is not None and not legend_breaks: legend_ticks = [float(v) for v in legend_ticks.split(',')] legend = renderer.get_legend(image_height=lh, breaks=legend_breaks, ticks=legend_ticks, max_precision=2)[0].to_image() elif renderer_type == 'unique': legend = renderer.get_legend(image_height=lh)[0].to_image() legend.save(os.path.join(output_directory, 'legend.png')) for filename in filenames: with rasterio.open(filename) as ds: print('Processing', filename) filename_root = os.path.split(filename)[1].replace('.nc', '') data = ds.read(1, masked=True) # # get transforms, assume last 2 dimensions on variable are spatial in row, col order # y_dim, x_dim = ds.variables[variable].dimensions[-2:] # y_len, x_len = data.shape[-2:] # coords = SpatialCoordinateVariables.from_dataset(ds, x_dim, y_dim)#, projection=Proj(src_crs)) # # if coords.y.is_ascending_order(): # data = data[::-1] # reproject_kwargs = None if dst_crs is not None: # TODO: extract this out into a general trefoil reprojection function ds_crs = ds.crs if not (src_crs or ds_crs): raise click.BadParameter( 'must provide src_crs to reproject', param='src_crs', param_hint='src_crs') dst_crs = {'init': dst_crs} src_crs = ds_crs if ds_crs else {'init': src_crs} left, bottom, top, right = ds.bounds dst_affine, dst_width, dst_height = calculate_default_transform( left, bottom, right, top, ds.width, ds.height, src_crs, dst_crs) dst_shape = (dst_height, dst_width) # proj_bbox = coords.bbox.project(Proj(dst_crs)) # # x_dif = proj_bbox.xmax - proj_bbox.xmin # y_dif = proj_bbox.ymax - proj_bbox.ymin # # total_len = float(x_len + y_len) # # Cellsize is dimension weighted average of x and y dimensions per projected pixel, unless otherwise provided # avg_cellsize = ((x_dif / float(x_len)) * (float(x_len) / total_len)) + ((y_dif / float(y_len)) * (float(y_len) / total_len)) # # cellsize = res or avg_cellsize # dst_affine = Affine(cellsize, 0, proj_bbox.xmin, 0, -cellsize, proj_bbox.ymax) # dst_shape = ( # max(int(ceil((y_dif) / cellsize)), 1), # height # max(int(ceil(x_dif / cellsize)), 1) # width # ) # TODO: replace with method in rasterio reproject_kwargs = { 'src_crs': src_crs, 'src_transform': ds.affine, 'dst_crs': dst_crs, 'dst_transform': dst_affine, 'resampling': getattr(Resampling, resampling), 'dst_shape': dst_shape } if anchors: # Reproject the bbox of the output to WGS84 full_bbox = BBox( (dst_affine.c, dst_affine.f + dst_affine.e * dst_shape[0], dst_affine.c + dst_affine.a * dst_shape[1], dst_affine.f), projection=Proj(dst_crs)) wgs84_bbox = full_bbox.project(Proj(init='EPSG:4326')) print('WGS84 Anchors: {0}'.format( [[wgs84_bbox.ymin, wgs84_bbox.xmin], [wgs84_bbox.ymax, wgs84_bbox.xmax]])) elif anchors: # Reproject the bbox of the output to WGS84 full_bbox = BBox(ds.bounds, projection=Proj(ds.crs)) wgs84_bbox = full_bbox.project(Proj(init='EPSG:4326')) print('WGS84 Anchors: {0}'.format( [[wgs84_bbox.ymin, wgs84_bbox.xmin], [wgs84_bbox.ymax, wgs84_bbox.xmax]])) image_filename = os.path.join(output_directory, '{0}.png'.format(filename_root)) render_image(renderer, data, image_filename, scale, reproject_kwargs=reproject_kwargs)
wid=default_args['-w'], ingis=default_args['-s'], outgis=default_args['-t']).strip()) # Read catogram file catomap = gpd.read_file(input_file) assert 'CACODE' in catomap.columns assert 'geometry' in catomap.columns catomap['district'] = catomap['CACODE'].str.slice(stop=1).astype('category') hex_size = float(default_args['-w']) # Find the centroid of each shape and change to hk80 hk80_grid = inProj = Proj(init=( 'EPSG:2326 +proj=tmerc ' '+lat_0=22.31213333333334 +lon_0=114.1785555555556 +k=1 +x_0=836694.05 ' '+y_0=819069.8 +ellps=intl +towgs84=-162.619,-276.959,-161.764,' '0.067753,-2.24365,-1.15883,-1.09425 +units=m +no_defs')) wgs_grid = Proj(init='epsg:4326') if default_args['-s'] == 'hk80': catomap['cent'] = catomap['geometry'].centroid else: catomap['cent'] = catomap['geometry'].centroid.apply( lambda geo: transform(wgs_grid, hk80_grid, geo.x, geo, y)) # Generate hexagon base grid map_grid = gen_hex_points(hex_size, catomap.cent.apply(lambda x: x.x).min(), catomap.cent.apply(lambda x: x.y).min(), catomap.cent.apply(lambda x: x.x).max() + 10,
def coord_regression(predictions_b, predictions, train, test, method): mean_error = [] if (method == 1): machine_learn = KNeighborsRegressor(n_neighbors=5, weights='distance') elif (method == 2): #machine_learn = MLPClassifier(solver='sgd',learning_rate = 'adaptive',verbose='true',activation='tanh',alpha=1e-5) machine_learn = MLPClassifier(solver='sgd', learning_rate='adaptive', verbose='false', activation='tanh', alpha=1e-5, max_iter=400) #THE BEST #machine_learn = MLPClassifier(hidden_layer_sizes=(100,5), solver='sgd',learning_rate = 'adaptive',verbose='true',activation='tanh',alpha=1e-5,max_iter=500) #model = MLPClassifier(learning_rate = 'adaptive') #solvers = ['lbfgs', 'sgd', 'adam'] #activations = ['identity', 'logistic', 'tanh', 'relu'] #max_its = [200,400,600] #machine_learn = GridSearchCV(estimator=model, param_grid=dict(activation =activations,max_iter=max_its),n_jobs=7) #GRID #for each building for j in range(3): new_train1 = train.loc[ train['BUILDINGID'] == j] #select for training only buildings with that label (0,1, or 2) ind = [x for x in range(len(predictions_b)) if predictions_b[x] == j ] #get the position of the samples that have building == i new_test1 = test.iloc[ind, :] if (ind): #for each floor for i in range(5): new_train2 = new_train1.loc[new_train1['FLOOR'] == i] if (not new_train2.empty): indexes = [ x for x in range(len(predictions)) if (predictions[x] == i and predictions_b[x] == j) ] #get the position of the samples that have building == i else: index = [] if (indexes): #if list is not empty X_train = new_train2.ix[:, 0:519] Y_train = new_train2[['LONGITUDE', 'LATITUDE']] machine_learn.fit(X_train, Y_train) #testing samples with prediction building == i new_test2 = test.iloc[indexes, :] X_test = new_test2.ix[:, 0:519] Y_test = new_test2[['LONGITUDE', 'LATITUDE']] #Turn into list predicts_lon_lat = machine_learn.predict(X_test).tolist() Y_test = Y_test.values.tolist() distance = [] for j in range(len(predicts_lon_lat)): #change the latitude and longitude unit myProj = Proj( "+proj=utm +zone=23K, +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs" ) lon_pred, lat_pred = myProj(predicts_lon_lat[j][0], predicts_lon_lat[j][1], inverse=True) lon_Y, lat_Y = myProj(Y_test[j][0], Y_test[j][1], inverse=True) #join in a unique list Y = [] Y.append(lon_Y) Y.append(lat_Y) predict = [] predict.append(lon_pred) predict.append(lat_pred) #The distance between the two latitudes is the error distance.append(vincenty(Y, predict).meters) print "distance" print distance #If you want to use haversine distance, uncomment the line below #print haversine(lon_Y, lat_Y, lon_pred, lat_pred) mean_error.append(np.mean(distance)) #print(np.mean(distance)) return np.mean(mean_error)
def from_coordinates(cls, x, y): long, lat = transform(Proj(init=EPSG_IN), Proj(init=EPSG_OUT), x, y) return cls(lat, long)
class Map(): projections = OrderedDict([ ('Spherical', Proj('+proj=ortho +lat_0=48 +lon_0=17')), ('Mercator', Proj(init='epsg:3395')), ('WGS84', Proj(init='epsg:3857')), ('ETRS89 - LAEA Europe', Proj("+init=EPSG:3035")) ]) def __init__(self, view): self.view = view self.proj = 'Spherical' self.ratio, self.offset = 1/1000, (0, 0) self.shapefile = join(self.view.controller.path_shapefiles, 'World countries (low resolution).shp') self.display = True # brush for water and lands self.water_brush = QBrush(QColor(64, 164, 223)) self.land_brush = QBrush(QColor(52, 165, 111)) self.land_pen = QPen(QColor(52, 165, 111)) # draw the map self.polygons = self.view.scene.createItemGroup(self.draw_polygons()) self.draw_water() def to_geographical_coordinates(self, x, y): px, py = (x - self.offset[0])/self.ratio, (self.offset[1] - y)/self.ratio return self.projections[self.proj](px, py, inverse=True) def to_canvas_coordinates(self, longitude, latitude): px, py = self.projections[self.proj](longitude, latitude) return px*self.ratio + self.offset[0], -py*self.ratio + self.offset[1] def draw_water(self): if self.proj in ('Spherical', 'ETRS89 - LAEA Europe'): cx, cy = self.to_canvas_coordinates(17, 48) # if the projection is ETRS89, we need the diameter and not the radius R = 6371000*self.ratio*(1 if self.proj == 'Spherical' else 2) earth_water = QtWidgets.QGraphicsEllipseItem(cx - R, cy - R, 2*R, 2*R) earth_water.setZValue(0) earth_water.setBrush(self.water_brush) self.polygons.addToGroup(earth_water) else: # we compute the projected bounds of the Mercator (3395) projection # upper-left corner x and y coordinates: ulc_x, ulc_y = self.to_canvas_coordinates(-180, 84) # lower-right corner x and y coordinates lrc_x, lrc_y = self.to_canvas_coordinates(180, -84.72) # width and height of the map (required for the QRectItem) width, height = lrc_x - ulc_x, lrc_y - ulc_y earth_water = QtWidgets.QGraphicsRectItem(ulc_x, ulc_y, width, height) earth_water.setZValue(0) earth_water.setBrush(self.water_brush) self.polygons.addToGroup(earth_water) def draw_polygons(self): sf = shapefile.Reader(self.shapefile) polygons = sf.shapes() for polygon in polygons: # convert shapefile geometries into shapely geometries # to extract the polygons of a multipolygon polygon = shapely.geometry.shape(polygon) # if it is a polygon, we use a list to make it iterable if polygon.geom_type == 'Polygon': polygon = [polygon] for land in polygon: qt_polygon = QtGui.QPolygonF() longitudes, latitudes = land.exterior.coords.xy for lon, lat in zip(longitudes, latitudes): px, py = self.to_canvas_coordinates(lon, lat) if px > 1e+10: continue qt_polygon.append(QtCore.QPointF(px, py)) polygon_item = QtWidgets.QGraphicsPolygonItem(qt_polygon) polygon_item.setBrush(self.land_brush) polygon_item.setPen(self.land_pen) polygon_item.setZValue(1) yield polygon_item def show_hide_map(self): self.display = not self.display self.polygons.show() if self.display else self.polygons.hide() def delete_map(self): self.view.scene.removeItem(self.polygons) def redraw_map(self): self.delete_map() self.polygons = self.view.scene.createItemGroup(self.draw_polygons()) self.draw_water() # replace the nodes at their geographical location self.view.move_to_geographical_coordinates()
def test_transform_no_error(): with pytest.warns(FutureWarning): pj = Proj(init="epsg:4555") pjx, pjy = pj(116.366, 39.867) with pytest.warns(DeprecationWarning): transform(pj, Proj(4326), pjx, pjy, radians=True, errcheck=True)
class Navigation(object): """Common navigation machinery used by different modules""" def __init__(self, beating_angle=45, utm_zone=30): """ position_ll : Position as a LatLon object heading : Compass heading beating_angle : Closest absolute angle relative to the wind that we can sail utm_zone : Zone number of the UTM system to use. Southampton is in zone 30, Portugal in zone 29. http://www.dmap.co.uk/utmworld.htm Distance calculations will be less accurate the further from the specified zone you are. """ self.projection = Proj(proj='utm', zone=utm_zone, ellps='WGS84') self.position_ll = ll = LatLon(50.8, 1.02) x, y = self.latlon_to_utm(ll.lat.decimal_degree, ll.lon.decimal_degree) self.position_xy = Point(x, y) self.heading = 0. self.wind_direction = 0. self.beating_angle = beating_angle def update_position(self, msg): self.position_ll = LatLon(msg.latitude, msg.longitude) x, y = self.latlon_to_utm(msg.latitude, msg.longitude) self.position_xy = Point(x, y) def latlon_to_utm(self, lat, lon): """Returns (x, y) coordinates in metres""" return self.projection(lon, lat) def utm_to_latlon(self, x, y): """Returns a LatLon object""" lon, lat = self.projection.inverse(x, y, inverse=True) return ll.LatLon(lat, lon) def update_heading(self, msg): self.heading = msg.data def update_wind_direction(self, msg): self.wind_direction = msg.data def absolute_wind_direction(self): """Convert apparent wind direction to absolute wind direction""" # This assumes that our speed is negligible relative to wind speed. return angleSum(self.heading, self.wind_direction) def angle_to_wind(self): """Calculate angle relative to wind (-180 to 180) Angle relative to wind is reversed from wind direction: if the wind is coming from 90, the angle relative to the wind is -90. """ wd = self.wind_direction if wd > 180: wd -= 360 return -wd def heading_to_wind_angle(self, heading): """Convert a compass heading (0-360) to an angle relative to the wind (+-180) """ res = (heading - self.absolute_wind_direction()) % 360 if res > 180: res -= 360 return res def wind_angle_to_heading(self, wind_angle): """Convert angle relative to the wind (+-180) to a compass heading (0-360). """ return angleSum(self.absolute_wind_direction(), wind_angle) def subscribe_topics(self): """Subscribe to ROS topics to keep this nav object up to date. Subscribes to /position, /heading and /wind_direction_apparent. """ from rospy import Subscriber from std_msgs.msg import Float32, Float64 from sensor_msgs.msg import NavSatFix Subscriber('/heading', Float32, self.update_heading) Subscriber('/wind_direction_apparent', Float64, self.update_wind_direction) Subscriber('/position', NavSatFix, self.update_position)
def test_is_exact_same_different_type(): assert not Proj("epsg:4326").is_exact_same(None)
def test_equals_different_type(): assert Proj("epsg:4326") != ""
def readLineCoordsFromMultiLineShp(shapefileFn, transNum): ''' # TAKEN FROM M. FAHNESTOCK'S L8_sample_frames... code. 08 sep 2016 # Unfinished as of 08 sep 2016 wha # 22 sep 2016 think it's finished wha # Mark's notes # use fiona to read in points along centerline in local projection (called PS here) # save the PS original points in sample_pts_PS, then reproject to 4326, push back into # coordinates of input Point object (f) and make a list of these (out_pts_ll) and when done save # them as a Shapely MultiPoint object ''' with fiona.open(shapefileFn, 'r') as c: # Initialize sample_pts_local = [] sample_pts_lon_lat = [] xList = [] yList = [] lonList = [] latList = [] # Get coordinate reference system from original shapefile original = Proj(c.crs) destination = Proj(init='EPSG:4326') # dest is WGS84 = EPSG 4326 f = c[transNum] # open specified feature (transect) props = f['properties'] for j in f['geometry']['coordinates']: x = j[0] y = j[1] xList.append(j[0]) yList.append(j[1]) sample_pts_local.append((x, y)) lon, lat = transform(original, destination, x, y) lonList.append(lon) latList.append(lat) sample_pts_lon_lat.append((lon, lat)) medLon = np.median(lonList) # get meadian longitude utmZone = utm.from_latlon(51.2, medLon)[2] # get utm zone epsgLocalUtm = 32600 + utmZone destination2 = Proj(init='EPSG:' + str(epsgLocalUtm)) easting, northing = transform(destination, destination2, lonList, latList) # Stick it together in a dict for storage dataOut = { 'inProps': props, 'shpFn': shapefileFn, 'xLocal': xList, 'yLocal': yList, 'lat': latList, 'lon': lonList, 'samplePtsXy': sample_pts_local, 'samplePtsLatLon': sample_pts_lon_lat, 'utmEasting': easting, 'utmNorthing': northing, 'utmZone': utmZone } return dataOut
def _load(filename, table, column, work_dir, server_url, capacity, usewith, srid=0): '''load pointclouds data using pdal and add metadata needed by lopocs''' # intialize flask application app = create_app() filename = Path(filename) work_dir = Path(work_dir) extension = filename.suffix[1:].lower() # laz uses las reader in PDAL extension = extension if extension != 'laz' else 'las' basename = filename.stem basedir = filename.parent pending('Creating metadata table') Session.create_pointcloud_lopocs_table() ok() pending('Reading summary with PDAL') json_path = os.path.join( str(work_dir.resolve()), '{basename}_{table}_pipeline.json'.format(**locals())) # tablename should be always prefixed if '.' not in table: table = 'public.{}'.format(table) cmd = "pdal info --summary {}".format(filename) try: output = check_output(shlex.split(cmd)) except CalledProcessError as e: fatal(e) summary = json.loads(output.decode())['summary'] ok() if 'srs' not in summary and not srid: fatal('Unable to find the spatial reference system, please provide a SRID with option --srid') if not srid: # find authority code in wkt string srid = re.findall('EPSG","(\d+)"', summary['srs']['wkt'])[-1] p = Proj(init='epsg:{}'.format(srid)) if p.is_latlong(): # geographic scale_x, scale_y, scale_z = (1e-6, 1e-6, 1e-2) else: # projection or geocentric scale_x, scale_y, scale_z = (0.01, 0.01, 0.01) offset_x = summary['bounds']['X']['min'] + (summary['bounds']['X']['max'] - summary['bounds']['X']['min']) / 2 offset_y = summary['bounds']['Y']['min'] + (summary['bounds']['Y']['max'] - summary['bounds']['Y']['min']) / 2 offset_z = summary['bounds']['Z']['min'] + (summary['bounds']['Z']['max'] - summary['bounds']['Z']['min']) / 2 reproject = "" if usewith == 'cesium': from_srid = srid # cesium only use epsg:4978, so we must reproject before loading into pg srid = 4978 reproject = """ {{ "type":"filters.reprojection", "in_srs":"EPSG:{from_srid}", "out_srs":"EPSG:{srid}" }},""".format(**locals()) # transform bounds in new coordinate system pini = Proj(init='epsg:{}'.format(from_srid)) pout = Proj(init='epsg:{}'.format(srid)) # recompute offset in new space and start at 0 pending('Reprojected bounds', nl=True) # xmin, ymin, zmin = transform(pini, pout, offset_x, offset_y, offset_z) xmin, ymin, zmin = transform(pini, pout, summary['bounds']['X']['min'], summary['bounds']['Y']['min'], summary['bounds']['Z']['min']) xmax, ymax, zmax = transform(pini, pout, summary['bounds']['X']['max'], summary['bounds']['Y']['max'], summary['bounds']['Z']['max']) offset_x, offset_y, offset_z = xmin, ymin, zmin click.echo('{} < x < {}'.format(xmin, xmax)) click.echo('{} < y < {}'.format(ymin, ymax)) click.echo('{} < z < {} '.format(zmin, zmax), nl=False) ok() pending('Computing best scales for cesium') # override scales for cesium if possible we try to use quantized positions scale_x = min(compute_scale_for_cesium(xmin, xmax), 1) scale_y = min(compute_scale_for_cesium(ymin, ymax), 1) scale_z = min(compute_scale_for_cesium(zmin, zmax), 1) ok('[{}, {}, {}]'.format(scale_x, scale_y, scale_z)) pg_host = app.config['PG_HOST'] pg_name = app.config['PG_NAME'] pg_port = app.config['PG_PORT'] pg_user = app.config['PG_USER'] pg_password = app.config['PG_PASSWORD'] realfilename = str(filename.resolve()) schema, tab = table.split('.') pending('Loading point clouds into database') with io.open(json_path, 'w') as json_file: json_file.write(PDAL_PIPELINE.format(**locals())) cmd = "pdal pipeline {}".format(json_path) try: check_call(shlex.split(cmd), stderr=DEVNULL, stdout=DEVNULL) except CalledProcessError as e: fatal(e) ok() pending("Creating indexes") Session.execute(""" create index on {table} using gist(pc_envelopegeometry(points)); alter table {table} add column morton bigint; select Morton_Update('{table}', 'points', 'morton', 128, TRUE); create index on {table}(morton); """.format(**locals())) ok() pending("Adding metadata for lopocs") Session.update_metadata( table, column, srid, scale_x, scale_y, scale_z, offset_x, offset_y, offset_z ) lpsession = Session(table, column) ok() # retrieve boundingbox fullbbox = lpsession.boundingbox bbox = [ fullbbox['xmin'], fullbbox['ymin'], fullbbox['zmin'], fullbbox['xmax'], fullbbox['ymax'], fullbbox['zmax'] ] if usewith == 'potree': lod_min = 0 lod_max = 5 # add schema currently used by potree (version 1.5RC) Session.add_output_schema( table, column, 0.01, 0.01, 0.01, offset_x, offset_y, offset_z, srid, potree_schema ) cache_file = ( "{0}_{1}_{2}_{3}_{4}.hcy".format( lpsession.table, lpsession.column, lod_min, lod_max, '_'.join(str(e) for e in bbox) ) ) pending("Building greyhound hierarchy") new_hcy = greyhound.build_hierarchy_from_pg( lpsession, lod_min, lod_max, bbox ) greyhound.write_in_cache(new_hcy, cache_file) ok() create_potree_page(str(work_dir.resolve()), server_url, table, column) if usewith == 'cesium': pending("Building 3Dtiles tileset") hcy = threedtiles.build_hierarchy_from_pg( lpsession, server_url, bbox ) tileset = os.path.join(str(work_dir.resolve()), 'tileset-{}.{}.json'.format(table, column)) with io.open(tileset, 'wb') as out: out.write(hcy.encode()) ok() create_cesium_page(str(work_dir.resolve()), table, column)
def __reproject_point(self, x, y, source_crs_epsg, dest_crs_epsg): source = Proj(init=source_crs_epsg) dest = Proj(init=dest_crs_epsg) dest_x, dest_y = transform(source, dest, x, y) return dest_x, dest_y
def main(raster, outputdir, verbose): """ Convert a raster file (e.g. GeoTIFF) into an HDF5 file. RASTER: Path to raster file to convert to hdf5. The HDF5 file has the following datasets: - Raster: (original image data) - Latitude: (vector or matrix of pixel latitudes) - Longitude: (vector or matrix of pixel longitudes) And the following attributes: - affine: The affine transformation of the raster - Various projection information. """ if verbose: print("Opening raster ...") # Read raster bands directly to Numpy arrays. # Much of this is from: # http://gis.stackexchange.com/questions/129847/ # obtain-coordinates-and-corresponding-pixel-values-from-geotiff-using # -python-gdal with rasterio.open(os.path.expanduser(raster)) as f: T0 = f.affine # upper-left pixel corner affine transform crs = f.crs p1 = Proj(crs) I = f.read() nanvals = f.get_nodatavals() # Make sure rasterio is always giving us a 3D array assert(I.ndim == 3) # This only works on lat-lon projections for now if not p1.is_latlong(): print("Error: This only works on spherical projections for now (YAGNI" " you know)...") exit(1) if verbose: print("Extracting coordinate sytem ...") # Get affine transform for pixel centres T1 = T0 * Affine.translation(0.5, 0.5) # Just find lat/lons of axis if there is no rotation/shearing # https://en.wikipedia.org/wiki/Transformation_matrix#Affine_transformations if (T1[1] == 0) and (T1[3] == 0): lons = T1[2] + np.arange(I.shape[2]) * T1[0] lats = T1[5] + np.arange(I.shape[1]) * T1[4] # Else, find lat/lons of every pixel! else: print("Error: Not yet tested... or even implemented properly!") exit(1) # Need to apply affine transformation to all pixel coords cls, rws = np.meshgrid(np.arange(I.shape[2]), np.arange(I.shape[1])) # Convert pixel row/column index (from 0) to lat/lon at centre rc2ll = lambda r, c: (c, r) * T1 # All eastings and northings (there a better way to do this) lons, lats = np.vectorize(rc2ll, otypes=[np.float, np.float])(rws, cls) # Permute layers to be more like a standard image, i.e. (band, lon, lat) -> # (lon, lat, band) I = (I.transpose([2, 1, 0]))[:, ::-1] lats = lats[::-1] # Mask out NaN vals if they exist if nanvals is not None: for v in nanvals: if v is not None: if verbose: print("Writing missing values") I[I == v] = np.nan # Now write the hdf5 if verbose: print("Writing HDF5 file ...") file_stump = os.path.basename(raster).split('.')[-2] hdf5name = os.path.join(outputdir, file_stump + ".hdf5") with h5py.File(hdf5name, 'w') as f: drast = f.create_dataset("Raster", I.shape, dtype=I.dtype, data=I) drast.attrs['affine'] = T1 for k, v in crs.items(): drast.attrs['k'] = v f.create_dataset("Latitude", lats.shape, dtype=float, data=lats) f.create_dataset("Longitude", lons.shape, dtype=float, data=lons) if verbose: print("Done!")
def describe(path_or_dataset): if isinstance(path_or_dataset, string_types): dataset = Dataset(path_or_dataset) else: dataset = path_or_dataset description = { 'dimensions': {}, 'variables': {}, 'attributes': get_ncattrs(dataset) } for dimension_name in dataset.dimensions: dimension = dataset.dimensions[dimension_name] description['dimensions'][dimension_name] = {'length': len(dimension)} for variable_name in dataset.variables: variable = dataset.variables[variable_name] if not variable.dimensions: # Do not collect info about dimensionless variables (e.g., CRS variable) continue dtype = str(variable.dtype) if "'" in dtype: dtype = dtype.split("'")[1] attributes = get_ncattrs(variable) variable_info = { 'attributes': attributes, 'dimensions': variable.dimensions, 'data_type': dtype, 'name': attributes.get('long_name') or attributes.get('standard_name') or variable_name } if dtype not in ('str', ): if len(variable.shape) > 2: # Avoid loading the entire array into memory by iterating along the first index (usually time) variable_info.update({ 'min': min(variable[i, :].min().item() for i in range(variable.shape[0])), 'max': max(variable[i, :].max().item() for i in range(variable.shape[0])) }) else: data = variable[:] variable_info.update({ 'min': data.min().item(), 'max': data.max().item() }) if variable_name in dataset.dimensions and dtype not in ('str', ): dimension_variable = dataset.variables[variable_name] if len(dimension_variable.dimensions ) == 1: # range dimensions don't make sense for interval interval = get_interval(dimension_variable) if interval: variable_info['interval'] = interval else: # Data variable proj4 = get_crs(dataset, variable_name) #extent if len(variable.dimensions) >= 2: x_variable_name = None y_variable_name = None time_variable_name = None for dimension_name in (x for x in variable.dimensions if x in dataset.variables): attributes = get_ncattrs(dataset.variables[dimension_name]) standard_name = attributes.get('standard_name', None) if standard_name in X_DIMENSION_STANDARD_NAMES or dimension_name in X_DIMENSION_COMMON_NAMES: x_variable_name = dimension_name elif standard_name in Y_DIMENSION_STANDARD_NAMES or dimension_name in Y_DIMENSION_COMMON_NAMES: y_variable_name = dimension_name elif standard_name in TIME_DIMENSION_STANDARD_NAMES or dimension_name in TIME_DIMENSION_COMMON_NAMES: if len(dataset.dimensions[dimension_name]) > 1: time_variable_name = dimension_name if x_variable_name and y_variable_name: if proj4 is None and is_geographic(dataset, variable_name): # Assume WGS84 proj4 = PROJ4_GEOGRAPHIC coordinates = SpatialCoordinateVariables( SpatialCoordinateVariable( dataset.variables[x_variable_name]), SpatialCoordinateVariable( dataset.variables[y_variable_name]), Proj(str(proj4)) if proj4 else None) variable_info['spatial_grid'] = { 'extent': coordinates.bbox.as_dict(), 'x_dimension': x_variable_name, 'x_resolution': coordinates.x.pixel_size, 'y_dimension': y_variable_name, 'y_resolution': coordinates.y.pixel_size } if time_variable_name: time_variable = dataset.variables[time_variable_name] time_info = { 'dimension': time_variable_name, } try: date_variable = DateVariable(time_variable) values = date_variable.datetimes time_info['extent'] = [ values.min().isoformat(), values.max().isoformat() ] time_info['interval_unit'] = date_variable.unit interval = get_interval(time_variable) if interval is not None: time_info['interval'] = interval except ValueError: pass variable_info['time'] = time_info if proj4: variable_info['proj4'] = proj4 description['variables'][variable_name] = variable_info return description
with open(config_dir + '/config.yml', 'r') as yamlfile: cfg = yaml.load(yamlfile) # Open incident push file file_incident_push = open(watch_dir + '/incident_push/incident_push.p', 'rb') incident_push = pickle.load(file_incident_push) # Open agency codes table file_agency_codes = csv.DictReader(open(config_dir + '/agency_codes.csv')) agency_codes = { rows['agency_code']: rows['city_desc'] for rows in file_agency_codes } # Define projections mi_south = Proj(init='epsg:3593', preserve_units=True ) # NAD 1983 StatePlane Michigan South, FIPS 2113 IntlFeet web_mercator = Proj(init='epsg:3857') # WGS 1984 Web Mercator Auxiliary Sphere wgs84 = Proj(init='epsg:4326') # WGS84 for i in incident_push: for agency in cfg['agencies'].keys(): if i['agency_code'] == cfg['agencies'][agency]['agency_code']: # Assign variable to web GIS ago_portal = cfg['agencies'][agency]['ago_portal'] ago_user = cfg['agencies'][agency]['ago_user'] ago_pass = cfg['agencies'][agency]['ago_pass'] gis = GIS(ago_portal, ago_user, ago_pass) # Assign variable to feature layers fl_fireincidents = gis.content.get( cfg['agencies'][agency]['flc_fireincidents']).layers[0]
#!/usr/bin/env python3 # -*- coding: utf-8 -*- ############################################################################### ############################################################################### from pyproj import Proj albers = Proj('+proj=aea +lon_0=105 +lat_1=25 +lat_2=47 +ellps=krass') albers_x, albers_y = albers(105, 36) albers_x, albers_y ############################################################################### utm = Proj(proj='utm', zone=48, ellps='krass') utm_x, utm_y = utm(105, 36) print(utm_x, utm_y) ############################################################################### from pyproj import transform to_utm_x, to_utm_y = transform(albers, utm, albers_x, albers_y) print(to_utm_x, to_utm_y)