def get_proj_dict(dem_dataset, epsg): dem_transform = {'upper_left_x': dem_dataset.GetGeoTransform()[0], 'x_res_m' : dem_dataset.GetGeoTransform()[1], 'x_rotation': dem_dataset.GetGeoTransform()[2], 'upper_left_y': dem_dataset.GetGeoTransform()[3], 'y_rotation': dem_dataset.GetGeoTransform()[4], 'y_res_m': dem_dataset.GetGeoTransform()[5], 'n_cols': dem_dataset.RasterXSize, 'n_rows': dem_dataset.RasterYSize} dem_transform['y_res_m'] *= -1 dem_transform['east_min'] = dem_transform['upper_left_x'] dem_transform['east_max'] = (dem_transform['x_res_m'] * dem_transform['n_cols'] + dem_transform['east_min']) dem_transform['north_max'] = dem_transform['upper_left_y'] dem_transform['north_min'] = (-1 * dem_transform['y_res_m'] * dem_transform['n_rows'] + dem_transform['north_max']) wgs84 = pj.Proj(init='epsg:4326') utm = pj.Proj(init='epsg:{}'.format(epsg)) dem_transform['lon_min'], dem_transform['lat_min'] = pj.transform(utm, wgs84, dem_transform['east_min'], dem_transform['north_min']) dem_transform['lon_max'], dem_transform['lat_max'] = pj.transform(utm, wgs84, dem_transform['east_max'], dem_transform['north_max']) return dem_transform
def getDims(dims): inProj = Proj(init='epsg:4326') # this is lon/lat outProj = Proj(init='epsg:3857') # 900913 coords = dims.split(" ") x0, y0 = transform(inProj, outProj, float(coords[0]), float(coords[1])) x1, y1 = transform(inProj, outProj, float(coords[2]), float(coords[3])) return str(x0) + " " + str(y0) + " " + str(x1) + " " + str(y1) # this is LCC
def get_idx_points_inside_polygon(plon, plat, poly_lon, poly_lat, pnt_idxs, buff_distance=10.): """ :parameter plon: Points longitude list :parameter plat: Points latitude list :parameter poly_lon: A list containing the longitude coordinates of the polygon vertexes :parameter poly_lat: A list containing the latitude coordinates of the polygon vertexes :returns: Indexes of the points inside the polygon """ selected_idx = [] # Fix the projections inProj = Proj(init='epsg:4326') outProj = Proj(init='epsg:3857') # Create polygon poly_xy = [] for lo, la in zip(poly_lon, poly_lat): x, y = transform(inProj, outProj, lo, la) poly_xy.append((x,y)) polygon = shapely.geometry.Polygon(poly_xy) # Add buffer buff = polygon.buffer(buff_distance) # Find points inside for lo, la, jjj in zip(plon, plat, pnt_idxs): x, y = transform(inProj, outProj, lo, la) point = shapely.geometry.Point((x,y)) if point.within(buff): selected_idx.append(jjj) return selected_idx
def centroid_planar(longlat_pairs): """ Centroid by projecting onto a plane. Inaccurate over long distances. """ # # projections prep from pyproj import Proj, transform p_geodetic = Proj(proj='latlong') p_flat = Proj('+init=EPSG:27700') # uk OS # # convert N = len(longlat_pairs) cartesians = np.zeros([N, 2]) # N-high for indx, pair in enumerate(longlat_pairs): lng, lat = map(float, pair) x, y = transform(p_geodetic, p_flat, lng, lat) cartesians[indx,:] = (x, y) x, y = np.average(cartesians, axis=0) lng, lat = transform(p_flat, p_geodetic, x, y) return (lng, lat)
def indices(self, srs, bbox, resolution): """buggiest method ever!""" srs = srs.strip().lower() if srs != self.srs: # do reprojection proj = Proj(init=srs) (min_x, min_y) = transform(proj, self.proj, bbox.min_x, bbox.min_y) (max_x, max_y) = transform(proj, self.proj, bbox.max_x, bbox.max_y) bbox = Bbox(min_x, min_y, max_x, max_y) # completely outside boundary? outside_x = bbox.min_x >= self.bbox.max_x or bbox.max_x <= self.bbox.min_x outside_y = bbox.min_y >= self.bbox.max_y or bbox.max_y <= self.bbox.min_y if outside_x or outside_y: return None # truncate bbox min_x = max(bbox.min_x, self.bbox.min_x) max_x = min(bbox.max_x, self.bbox.max_x) min_y = max(bbox.min_y, self.bbox.min_y) max_y = min(bbox.max_y, self.bbox.max_y) cellsize_x = float(self.cellsize_x(resolution)) cellsize_y = float(self.cellsize_y(resolution)) # Note: upside-down rows compared to coordinates row_from = floor( (self.bbox.max_y - max_y) / cellsize_y ) row_to = floor( (self.bbox.max_y - min_y) / cellsize_y + 1.0 ) col_from = floor( (min_x - self.bbox.min_x) / cellsize_x ) col_to = floor( (max_x - self.bbox.min_x) / cellsize_x + 1.0 ) return (int(row_from), int(row_to), int(col_from), int(col_to))
def get_map_geo(): #przychodza w EPSG:3857 if request.method == "GET": print "Heeelo" p3857 = pyproj.Proj(init="EPSG:3857") p4326 = pyproj.Proj(init="EPSG:4326") _long_min = request.args.get('long_min', None) _lat_min = request.args.get('lat_min', None) #_geo_diff = request.args.get('geo_diff', geo_diff) _long_max = request.args.get('long_max', None) _lat_max = request.args.get('lat_max', None) logger.debug(_long_min, _lat_min,) _long_min, _lat_min = pyproj.transform(p3857, p4326, _long_min, _lat_min) #logger.debug(str(_long_min), str(_lat_min), str(_long_max), str(_lat_max)) _long_max, _lat_max = pyproj.transform(p3857, p4326, _long_max, _lat_max) print "bede preparowal" _wms_params = prepare_params_to_request_4326(_lat_min, _long_min, _lat_max, _long_max) print "spreparowalem " + str(_wms_params) response = get_map_image_in_b64(server_topo, _wms_params, "topo.jpg") print "przygotowuje odpowiedz" return Response(status=200) else: return Response(status=400)
def GKtoUTM(R, H=None, zone=32, gk=None): """Transforms any Gauss-Krueger to UTM autodetect GK zone from offset.""" if gk is None: if H is None: rr = R[0][0] else: if isinstance(R, list) or isinstance(R, tuple): rr = R[0] else: rr = R gkzone = int(floor(rr * 1e-6)) print(gkzone) if gkzone <= 0 or gkzone >= 5: print("cannot detect valid GK zone") gk = Proj(init="epsg:"+str(31464+gkzone)) if H is None: # two-column matrix lon, lat = transform(gk, wgs84, R[0], R[1]) else: lon, lat = transform(gk, wgs84, R, H) utm = Proj(proj='utm', zone=zone, ellps='WGS84') # UTM return utm(lon, lat)
def test_indices(self): """docstring for test_indices""" srs = 'epsg:25832' bbox = self.bbox self.assertEqual(self.ts.indices(srs=srs, bbox=bbox, resolution=1), (0,4,0,8)) # lower left bbox = Bbox(500000, 5000000, 500999, 5000487) self.assertEqual(self.ts.indices(srs=srs, bbox=bbox, resolution=1), (2,4,0,4)) # single cell bbox = Bbox(500010, 5000900, 500020, 5000910) self.assertEqual(self.ts.indices(srs=srs, bbox=bbox, resolution=1), (0,1,0,1)) # outside bbox = Bbox(200000, 1000000, 300000, 2000000) self.assertEqual(self.ts.indices(srs=srs, bbox=bbox, resolution=1), None) # trunc bbox = Bbox(200000, 1000000, 700000, 7000000) self.assertEqual(self.ts.indices(srs=srs, bbox=bbox, resolution=1), (0,4,0,8)) # reproject srs = 'epsg:4326' utm = proj.Proj(init='epsg:25832') geo = proj.Proj(init='epsg:4326') min_x, min_y = proj.transform(utm, geo, 500010, 5000900) max_x, max_y = proj.transform(utm, geo, 500020, 5000910) bbox = Bbox(min_x, min_y, max_x, max_y) print "Bbox: %f, %f, %f, %f, srs: %s" % (bbox.min_x, bbox.min_y, bbox.max_x, bbox.max_y, srs) self.assertEqual(self.ts.indices(srs=srs, bbox=bbox, resolution=1), (0,1,0,1))
def distance(x1, y1, x2, y2): x1, y1 = transform(wgs84_projection, metric_projection, float(x1), float(y1), radians=False) x2, y2 = transform(wgs84_projection, metric_projection, float(x2), float(y2), radians=False) #print x1, y1, x2, y2 return Point(x1, y1).distance(Point(x2, y2))
def get_extrema(data,**kwargs): transform = False for key,value in kwargs.iteritems(): if key == 'transform': transform = value north = data.top south = data.bottom west = data.left east = data.right extrema = {'n':north,'s':south,'w':west,'e':east} # newcorners ur_point = [extrema['e'],extrema['n']] ll_point = [extrema['w'],extrema['s']] if transform == True: p1 = pyproj.Proj(init='epsg:'+str(3857)) p2 = pyproj.Proj(init='epsg:'+str(4326)) ur_point = pyproj.transform(p1,p2,ur_point[0],ur_point[1]) ll_point = pyproj.transform(p1,p2,ll_point[0],ll_point[1]) print ll_point extrema = {'n':ur_point[1],'s':ll_point[1],'w':ll_point[0],'e':ur_point[0]} print extrema return extrema
def degreesToMeters(arc): projWGS84 = Proj(proj='latlong', datum='WGS84') projLV03 = Proj(init='epsg:21781') chCenterWGS84 = [8.3, 46.85] p1 = transform(projWGS84, projLV03, *chCenterWGS84) p2 = transform(projWGS84, projLV03, chCenterWGS84[0] + arc, chCenterWGS84[1]) return p2[0] - p1[0]
def get_mbb_and_srs(layerDict): """Deprecated, please use get_crs and get_mbb""" #This function is unsatisfactory, it should actually be two different functions, one for the MBB and one for the SRS. Let's do that. for name in layerDict: with fiona.open(layerDict[name]['file']) as s: if 'minx' not in locals(): minx, miny, maxx, maxy = s.bounds CRS = s.crs #very unclean way to get properly formatted SRS vector = ogr.GetDriverByName('ESRI Shapefile') src_ds = vector.Open(layerDict[name]['file']) src_lyr = src_ds.GetLayer(0) srs = osr.SpatialReference() srs.ImportFromWkt(src_lyr.GetSpatialRef().__str__()) elif s.crs == CRS: x1, y1, x2, y2 = s.bounds if x1 < minx: minx = x1 if y1 < miny: miny = y1 if x2 > maxx: maxx = x2 if y2 > maxy: maxy = y2 else: #raise NameError('Layer: ' + str(name) + ' is of wrong CRS: ' + str(s.crs) + 'desired CRS: ' + str(CRS)) x1, y1, x2, y2 = s.bounds proj1=pyproj.Proj(s.crs) proj2=pyproj.Proj(CRS,preserve_units=True) x1, y1 = pyproj.transform(proj1,proj2,x1,y1) x2, y2 = pyproj.transform(proj1,proj2,x2,y2) if x1 < minx: minx = x1 if y1 < miny: miny = y1 if x2 > maxx: maxx = x2 if y2 > maxy: maxy = y2 return (minx,miny,maxx,maxy),srs
def get_image_tile(raster, x, y, z): try: bound = bounds(x, y, z) with rasterio.open(raster) as src: x_res, y_res = src.transform[0], src.transform[4] p1 = Proj({'init': 'epsg:4326'}) p2 = Proj(**src.crs) # project tile boundaries from lat/lng to source CRS tile_ul_proj = transform(p1, p2, bound.west, bound.north) tile_lr_proj = transform(p1, p2, bound.east, bound.south) # get origin point from the TIF tif_ul_proj = (src.bounds.left, src.bounds.top) # use the above information to calculate the pixel indices of the window top = int((tile_ul_proj[1] - tif_ul_proj[1]) / y_res) left = int((tile_ul_proj[0] - tif_ul_proj[0]) / x_res) bottom = int((tile_lr_proj[1] - tif_ul_proj[1]) / y_res) right = int((tile_lr_proj[0] - tif_ul_proj[0]) / x_res) window = ((top, bottom), (left, right)) # read the first three bands (assumed RGB) of the TIF into an array data = np.empty(shape=(3, 256, 256)).astype(src.profile['dtype']) for k in (1, 2, 3): src.read(k, window=window, out=data[k - 1], boundless=True) return Image.fromarray(np.moveaxis(data, 0, -1), mode='RGB') except Exception as err: raise TileNotFoundError(err)
def tricontourf_canvas(topology, datasetnc, request): """ topology - netcdf topology object dataset - netcdf dataset object request - original http request """ import wms_handler xmin, ymin, xmax, ymax = wms_handler.get_bbox(request) #proj = get_pyproj(request) #lonmin, latmin = proj(xmin, ymin, inverse=True) #lonmax, latmax = porj(xmax, ymax, inverse=True) CRS = get_pyproj(request) lonmin, latmin = pyproj.transform(CRS, EPSG4326, xmin, ymin) lonmax, latmax = pyproj.transform(CRS, EPSG4326, xmax, ymax) #logger.info("lonmin, latmin: {0} {1}".format(lonmin, latmin)) #logger.info("lonmax, latmax: {0} {1}".format(lonmax, latmax)) #compute triangular subset lon = topology.nodes[:,0] lat = topology.nodes[:,1] latlon_sub_idx = get_lat_lon_subset_idx(topology.nodes[:,0], topology[:,1], lonmin, latmin, lonmax, latmax) nv_sub_idx = get_nv_subset_idx(topology.faces[:], sub_idx)
def project(self, target_projection, edge_points=9): """ target_projection must be a pyproj projection. Densifies the edges with edge_points points between corners, and projects all of them. Returns the outer bounds of the projected coords. Note: beware projection issues when going to projections that don't fully encapsulate the world domain (e.g., Web Mercator has singularities above and below latitudes of ~ 85). """ assert self.projection and isinstance(target_projection, Proj) if target_projection.srs == self.projection.srs: return self.clone() if edge_points < 2: # use corners only x_values, y_values = transform(self.projection, target_projection, [self.xmin, self.xmax], [self.ymin, self.ymax]) return BBox((x_values[0], y_values[0], x_values[1], y_values[1]), projection=target_projection) samples = range(0, edge_points) xstep = float(self.xmax-self.xmin)/(edge_points-1) ystep = float(self.ymax-self.ymin)/(edge_points-1) x_values = [] y_values = [] for i, j in product(samples, samples): x_values.append(self.xmin + xstep * i) y_values.append(self.ymin + ystep * j) # TODO: check for bidrectional consistency, as is done in ncserve BoundingBox.project() method x_values, y_values = transform(self.projection, target_projection, x_values, y_values) return BBox((min(x_values), min(y_values), max(x_values), max(y_values)), target_projection)
def getBoundsByAspect(bbList,aspect,projSrc,projDest): ''' Return bounds adjusted for aspect ratio ''' bb = bbList bl = pyproj.transform(projSrc,projDest,bb[0],bb[1]) tr = pyproj.transform(projSrc,projDest,bb[2],bb[3]) projW = tr[0]-bl[0] projH = tr[1]-bl[1] projcx = (tr[0]+bl[0])/2 projcy = (tr[1]+bl[1])/2 pbb=[0,0,0,0] if projW/projH > aspect: pbb[0] = bl[0] pbb[2] = tr[0] pbb[1] = projcy-projW*0.5/aspect pbb[3] = projcy+projW*0.5/aspect else: pbb[1] = bl[1] pbb[3] = tr[1] pbb[0] = projcx-projH*0.5*aspect pbb[2] = projcx+projH*0.5*aspect nbl = pyproj.transform(projDest,projSrc,pbb[0],pbb[1]) ntr = pyproj.transform(projDest,projSrc,pbb[2],pbb[3]) return [nbl[0],nbl[1],ntr[0],ntr[1]]
def _transform_polygon(source_prj: pyproj.Proj, target_prj: pyproj.Proj, conservation_ratio: float, polygon: Union[Polygon, MultiLineString]) \ -> Union[Point, Polygon, MultiLineString]: must_reproject = source_prj is not None must_pointify = conservation_ratio == 0.0 must_simplify = 0.0 <= conservation_ratio < 1.0 if not must_reproject and not must_simplify: return polygon if must_pointify: ring_coords = [] for ring in polygon: ring_coords.extend(ring) x = np.array([coord[0] for coord in ring_coords]) y = np.array([coord[1] for coord in ring_coords]) px, py = np.zeros(1, dtype=x.dtype), np.zeros(1, dtype=y.dtype) pointify_geometry(x, y, px, py) if must_reproject: px, py = pyproj.transform(source_prj, target_prj, px, py) return float(px[0]), float(py[0]) else: transformed_polygon = [] for ring in polygon: x = np.array([coord[0] for coord in ring]) y = np.array([coord[1] for coord in ring]) if must_simplify: x, y = simplify_geometry(x, y, conservation_ratio) if must_reproject: x, y = pyproj.transform(source_prj, target_prj, x, y) transformed_polygon.append([(float(x), float(y)) for x, y in zip(x, y)]) return transformed_polygon
def load_grid(filename, proj_lat_long, proj_meters): print "loading:", filename grid_params = {} execfile(filename, grid_params) lon_min = grid_params['lon_min'] lon_max = grid_params['lon_max'] lat_min = grid_params['lat_min'] lat_max = grid_params['lat_max'] # project from lat-long ( x_min, y_min ) = pyproj.transform( proj_lat_long, proj_meters, lon_min, lat_min ) ( x_max, y_max ) = pyproj.transform( proj_lat_long, proj_meters, lon_max, lat_max ) grid = np.zeros( (grid_params['num_z'], grid_params['num_lat'], grid_params['num_lon']) , np.double) return(grid, x_min, x_max, y_min, y_max, grid_params['z_min'], grid_params['z_max'], )
def convert_points(pts, dst): """ Move (translate) points to a different origin point Parameters: pts -- list of points as WGS84 (lon,lat) pairs ref -- new reference point, first element of pts will be moved there Returns: list of new (lat, lon) coordinates """ origin = pts[0] utm_from = pyproj.Proj("+proj=utm +zone=%d +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs" % utm_zone(origin[0])) utm_to = pyproj.Proj("+proj=utm +zone=%d +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs" % utm_zone(dst[0])) wgs84 = pyproj.Proj("+init=EPSG:4326") pts_utm_from = [pyproj.transform(wgs84, utm_from, *p) for p in pts] origin_utm_from = pyproj.transform(wgs84, utm_from, *origin) dst_utm_to = pyproj.transform(wgs84, utm_to, *dst) pts_utm_to = [] for p in pts_utm_from: x = dst_utm_to[0] + (p[0] - origin_utm_from[0]) y = dst_utm_to[1] + (p[1] - origin_utm_from[1]) pts_utm_to.append((x,y)) return [pyproj.transform(utm_to, wgs84, *p) for p in pts_utm_to]
def reproject(self, crsish): in_proj = Proj(self.crs) coll = self.collection() out_schema = coll.schema.copy() out_crs = guess_crs(crsish) tmpds = self.tempds("reproject_%s" % crsish) with fiona.collection(tmpds, "w", "ESRI Shapefile", out_schema, crs=out_crs) as out_collection: out_proj = Proj(out_collection.crs) for in_feature in coll: out_feature = in_feature.copy() if in_feature['geometry']['type'] == "Polygon": new_coords = [] for ring in in_feature['geometry']['coordinates']: x2, y2 = transform(in_proj, out_proj, *zip(*ring)) new_coords.append(zip(x2, y2)) out_feature['geometry']['coordinates'] = new_coords elif in_feature['geometry']['type'] == "Point": x2, y2 = transform(in_proj, out_proj, *in_feature['geometry']['coordinates']) out_feature['geometry']['coordinates'] = x2, y2 out_collection.write(out_feature) return Layer(tmpds)
def create_circle(self, source, dist=1000, n_segments=20): """ Create a circle around source in a given distance and with the given number of segments Parameters ---------- source : Point-instance dist : float the distance around the point n_segments : int the number of segments of the (nearly) circle """ if source.x is None: source_x, source_y = transform(self.p1, self.p2, source.lon, source.lat) else: source_x, source_y = source.x, source.y angel = np.linspace(0, np.pi*2, n_segments) x = source_x + dist * np.cos(angel) y = source_y + dist * np.sin(angel) lon, lat = transform(self.p2, self.p1, x, y) destinations = np.vstack([lon, lat]).T return destinations
def contains(x1, y1, x2, y2, radius, origin1=WGS84, origin2=WGS84, destination=PA_SP_SOUTH): ''' :param x: x coordinate or longitude :param y: y coordiante or latitude :param radius: radius in miles :param projection: :return: ''' # project input x1,y1 to PA state plane try: x, y = pyproj.transform(pyproj.Proj(origin1), pyproj.Proj(destination, preserve_units=True), x1, y1) # get circle from first input p = Point(x, y) circle = p.buffer(radius * MILES) # project x2, y2 to same plane x, y = pyproj.transform(pyproj.Proj(origin2), pyproj.Proj(destination, preserve_units=True), x2, y2) p = Point(x, y) return circle.contains(p) except: return False
def saveKML(self, kmlFile): """ Saves a KML template to use with google earth. Assumes x/y coordinates are lat/long, and creates an overlay to display the heatmap within Google Earth. kmlFile -> output filename for the KML. """ if self.img is None: raise Exception("Must first run heatmap() to generate image file.") tilePath = os.path.splitext(kmlFile)[0] + ".png" self.img.save(tilePath) if self.override: ((west, south), (east, north)) = self.area else: ((west, south), (east, north)) = self._ranges() #convert overlay BBOX if required if use_pyproj and self.srcepsg is not None and self.srcepsg != 'EPSG:4326': source = pyproj.Proj(init=self.srcepsg) dest = pyproj.Proj(init='EPSG:4326') (east,south) = pyproj.transform(source,dest,east,south) (west,north) = pyproj.transform(source,dest,west,north) bytes = self.KML % (tilePath, north, south, east, west) fh = open(kmlFile, "w") fh.write(bytes) fh.close()
def extent(self): """Return extent of the image as a dict with keys minx, maxx, miny, maxy, in Google projection.""" data = gdal.Open(self.filename) pixelsx = data.RasterXSize pixelsy = data.RasterYSize startx, dxx, dyx, starty, dxy, dyy = data.GetGeoTransform() endx = startx + (pixelsx - 1) * dxx endy = starty - (pixelsy - 1) * dyy startx, starty = transform( RD_PROJECTION, GOOGLE_PROJECTION, startx, starty) endx, endy = transform( RD_PROJECTION, GOOGLE_PROJECTION, endx, endy) if startx > endx: startx, endx = endx, startx if starty > endy: starty, endy = endy, starty return { 'minx': startx, 'maxx': endx, 'miny': starty, 'maxy': endy, }
def get_buscar_ruta(self, **kwargs): values = json.loads(kwargs['rutas_wp']) google = pyproj.Proj("+init=EPSG:3857") gps = pyproj.Proj("+init=EPSG:4326") start = pyproj.transform(gps, google, values['start']['lng'], values['start']['lat']) finish = pyproj.transform(gps, google, values['end']['lng'], values['end']['lat']) sql = """SELECT id FROM movilidad_sostenible_oferta o WHERE ST_DWithin(o.shape, ST_SetSRID(ST_MakePoint(%s, %s), 900913), 500) AND ST_DWithin(o.shape, ST_SetSRID(ST_MakePoint(%s, %s), 900913), 500) """ # Busca rutas que esten a 500 metros de los puntos origen y destino seleccionados params = (start[0], start[1], finish[0], finish[1]) request.env.cr.execute(sql, params) res = request.env.cr.fetchall() #print values['start']['lng'], values['start']['lat'] #print values['end']['lng'], values['end']['lat'] #print sql #print params #print res rutas_ids = [ i[0] for i in res ] rutas_model = request.env['movilidad_sostenible.oferta'] rutas = rutas_model.browse(rutas_ids) #print rutas values.update(kwargs=kwargs.items()) return http.request.render('movilidad_sostenible.lista_rutas_ofertar', { 'lista_ofertas': rutas, })
def check_grid_consistency(grid1,grid2,proj1,proj2): ''' Given two 2D (mesh)grids and their respective projections, perform a number of checks to verify their consistency ''' def print_diffs(xt,yt,x2,y2): print 'Total accumulated longitude mismatch:',(xt-x2).sum() print 'Total accumulated latitude mismatch:',(yt-y2).sum() print 'Average longitudinal grid mismatch:',(xt-x2).mean() print 'Average latitudinal mismatch:',(yt-y2).mean() print 'Maximum longitudinal grid mismatch:',(xt-x2).max(),np.unravel_index((xt-x2).argmax(),xt.shape) print 'Maximum latitudinal mismatch:',(yt-y2).max(),np.unravel_index((yt-y2).argmax(),yt.shape),'\n' return for grid in ['mass_grid','u_grid','v_grid']: print 'Performing transformation on the %s'%grid x1,y1 = grid1['mass_grid'] x2,y2 = grid2['mass_grid'] # Transform proj1 to proj 2 print 'Transform proj1 to proj2 (probably LCC to lat/lon)' xt,yt = pyproj.transform(proj1,proj2,x1,y1) print_diffs(xt,yt,x2,y2) # Transform proj2 to proj 1 print 'Transform proj2 to proj1 (probably lat/lon to LCC)' xt,yt = pyproj.transform(proj2,proj1,x2,y2) print_diffs(xt,yt,x1,y1) return
def get_kml_poly(trans): outProj = Proj(init='EPSG:4326') inProj = Proj(init='EPSG:3857') kml = dict() kml['lonUL'], kml['latUL'] = transform(inProj, outProj, trans['lonUL'], trans['latUL']) kml['lonUR'], kml['latUR'] = transform(inProj, outProj, trans['lonUR'], trans['latUR']) kml['lonLL'], kml['latLL'] = transform(inProj, outProj, trans['lonLL'], trans['latLL']) kml['lonLR'], kml['latLR'] = transform(inProj, outProj, trans['lonLR'], trans['latLR']) return """ <Polygon> <extrude>1</extrude> <altitudeMode>clampToGround</altitudeMode> <outerBoundaryIs> <LinearRing> <coordinates> {lonUL},{latUL},0 {lonUR},{latUR},0 {lonLR},{latLR},0 {lonLL},{latLL},0 {lonUL},{latUL},0 </coordinates> </LinearRing> </outerBoundaryIs> </Polygon> """.format(**kml)
def _limit(self, lon, lat, target_cs): # TODO: lat long boundaries are not rectangular data_proj = Proj("+init=EPSG:4326") # WGS84 target_proj = Proj(target_cs) # Find bounding box in ERA projection bbox = self.bounding_box bb_proj = transform(target_proj, data_proj, bbox[0], bbox[1]) lon_min, lon_max = min(bb_proj[0]), max(bb_proj[0]) lat_min, lat_max = min(bb_proj[1]), max(bb_proj[1]) #print(lon_min,lon_max,lat_min,lat_max) # Limit data lon_upper = lon >= lon_min lon_lower = lon <= lon_max lat_upper = lat >= lat_min lat_lower = lat <= lat_max lon_inds = np.nonzero(lon_upper == lon_lower)[0] lat_inds = np.nonzero(lat_upper == lat_lower)[0] # Masks lon_mask = lon_upper == lon_lower lat_mask = lat_upper == lat_lower #print (lon_inds,lat_inds) #print (lon[lon_inds],lat[lat_inds]) if lon_inds.size == 0: raise ERAInterimDataRepositoryError("Bounding box longitudes don't intersect with dataset.") if lat_inds.size == 0: raise ERAInterimDataRepositoryError("Bounding box latitudes don't intersect with dataset.") x, y = transform(data_proj, target_proj, *np.meshgrid(lon[lon_inds], lat[lat_inds])) return x, y, (lon_mask, lat_mask), (lon_inds, lat_inds)
def reproject_cutline_gmerc(src_proj, points): if points: points = densify_linestring(points) points1 = zip(*pyproj.transform(src_proj, proj_gmerc, *zip(*points))) points2 = zip(*pyproj.transform(src_proj, proj_gmerc_180, *zip(*points))) return points1 if calc_area(points1) <= calc_area(points2) else points2 return []
def _get_projected_extent(self, src_srs, target_srs, edge_points=9): """ Densifies the edges with edge_points points between corners, and projects all of them. Returns the outer bounds of the projected coords. Geographic latitudes must be bounded to -85.0511 <= y <= 85.0511 prior to calling this or things will fail! """ srcProj = Proj(init=src_srs) if src_srs.strip().lower().startswith('epsg:') else Proj(str(src_srs)) targetProj = Proj(init=target_srs) if ':' in target_srs else Proj(str(target_srs)) if edge_points < 2: # cannot densify (calculations below will fail), just use corners x_values, y_values = transform(srcProj, targetProj, [self.xmin, self.xmax], [self.ymin, self.ymax]) return x_values[0], y_values[0], x_values[1], y_values[1] samples = range(0, edge_points) x_diff, y_diff = self.get_dimensions() xstep = x_diff/(edge_points-1) ystep = y_diff/(edge_points-1) x_values = [] y_values = [] for i, j in product(samples, samples): x_values.append(self.xmin + xstep * i) y_values.append(self.ymin + ystep * j) # TODO: check for bidrectional consistency, as is done in ncserve BoundingBox.project() method x_values, y_values = transform(srcProj, targetProj, x_values, y_values) return min(x_values), min(y_values), max(x_values), max(y_values)
def rocket(thrust_bias: np.ndarray): assert len(thrust_bias) == 24, "Bad guide length." # Covert ang to rads def rad(ang): return (ang / 360) * 2 * (3.1415926) # air density (simple model based on nasa function online) def air_density(alt): if alt <= 11000: T = 15.04 - (0.00649 * alt) p = 101.29 * math.pow((T + 273.1) / 288.08, 5.256) elif alt <= 25000: T = -56.46 p = float(22.65 * (math.exp(1.73 - 0.000157 * alt))).real else: T = -131.21 + (0.00299 * alt) p = 2.488 * (((T + 273.1) / 216.6)**-11.388).real d = p / (0.2869 * (T + 273.1)) return d def alt(Ex, Ey, Ez): ecef = pyproj.Proj(proj='geocent', ellps='WGS84', datum='WGS84') lla = pyproj.Proj(proj='latlong', ellps='WGS84', datum='WGS84') _, _, alt = pyproj.transform(ecef, lla, Ex, Ey, Ez, radians=True) return alt def grav_force(Ex, Ey, Ez, m): #lat = rad(lat) G = -6.67408 * (1 / (10**11)) # Gravitational Constant (m^3 kg^-1 s^-2) M = 5.972 * (10**24) # Earth Mass (kg) #a = 6398137 # equatoral radius #b = 6356752 # polar radius #R = math.sqrt((math.pow(math.pow(a, 2) * math.cos(lat), 2) + (math.pow(math.pow(b, 2) * math.sin(lat), 2))) / ( # math.pow(a * math.cos(lat), 2) + (math.pow(b * math.sin(lat), 2)))) # Radius of earth (m) r = (Ex**2 + Ey**2 + Ez**2)**0.5 F = (G * M * m) / (r**2) # Force of gravity (N) F_z = F * Ez / r F_x = F * (Ex / ((Ex**2 + Ey**2)**0.5)) F_y = F * (Ey / ((Ex**2 + Ey**2)**0.5)) print(F_x, F_y, F_z, sep='\t') return F_x, F_y, F_z # in the -r direction def drag_force(Ex, Ey, Ez, Evx, Evy, Evz): cd = 0.94 # coefficent of drag a = 0.00487 # cross sectional area m^2 p = air_density(alt(Ex, Ey, Ez)) # air density with respect to alt # drag = (1/2)*p*v_sqrd*cd*a*(vy/(math.sqrt(v))) v_sqrd = (Evx**2) + (Evy**2) + (Evz**2) if Evx == 0: Ex_drag = 0 else: Ex_drag = (1 / 2) * p * v_sqrd * cd * a * (-Evx / (math.sqrt(v_sqrd))) if Evy == 0: Ey_drag = 0 else: Ey_drag = (1 / 2) * p * v_sqrd * cd * a * ( -Evy / (math.sqrt(Evx**2 + Evy**2))) if Evz == 0: Ez_drag = 0 else: Ez_drag = (1 / 2) * p * v_sqrd * cd * a * ( -Evz / (math.sqrt(Evx**2 + Evy**2))) return Ex_drag, Ey_drag, Ez_drag # Net Force def net_force(Ex, Ey, Ez, Evx, Evy, Evz, m): Fx_drag, Fy_drag, Fz_drag = drag_force(Ex, Ey, Ez, Evx, Evy, Evz) Fx_grav, Fy_grav, Fz_grav = grav_force(Ex, Ey, Ez, m) Fx = Fx_drag + Fx_grav Fy = Fy_drag + Fy_grav Fz = Fz_drag + Fz_grav return Fx, Fy, Fz ##def lift_force lift -> pitching moment by reference length ##def side_force side -> yaw moment by reference length ##Areodynaminc forces are applied at center of pressfrom math import * # Need to find ideal coefficient of drag # Diameter assumed 0.3m # Burn time 175 s +- 20 s # avg thrust 1540 N # Total mass 700 kg # Propellant mass 1st stage 85% of first stage mass -> 510 kg +- x # Total 2nd stage 98 kg # 2nd Stage propellant 83 kg ## GOAL FOR FINAL CODE (this refers to the original code, this does not apply here at least in the foreseeable future) # Iterate through launch angels to minimize delta V in the y(rocket frame) direction, to 150km # after thrust, maximize delta v in the x(rocket frame) direction # Minimize time for initial trust burn as third priority # looking for initial launch angle, relative angle after thrust is 0, final velocity ## Inertial Earth Frame. Reference fram centered at the earth center of gravity (z-up, x-forward, y-right) # a = 6398137 # equatoral radius # b = 6356752 # polar radius # radius = R = math.sqrt((math.pow(math.pow(a, 2) * math.cos(latitude), 2) + (math.pow(math.pow(b, 2) * math.sin(latitude), 2))) / ( # math.pow(a * math.cos(latitude), 2) + (math.pow(b * math.sin(latitude), 2)))) # Radius of earth (m) # Ez = radius * sin(latitude) # Zero at earths center of gravity, going up through north pole # Ex = radius * cos(latitude) * cos(longitude) # Zero at earths center of gravity, going through equator forward # Ey = radius * cos(latitude) * sin(longitude) # Zero at earths center of gravity, going through equator right # with open('LLA.csv', 'r') as f: # # next(f) # # reader = csv.reader(f, 'excel') # # data = [] # # for row in reader: # # data.append(float(row[0])) # # latitude = rad(data[0]) # # longitude = rad(data[1]) # # altitude = data[2] # This is not the same as in the original code (just minor modifications). altitude = float(0) latitude = rad(float(28.5729)) longitude = rad(float(80.659)) #Altitude,Latitude,Longitude #0,28.5729,80.659 # Black Rock Navada. From 25km. 45 degrees from out of earth, due east. # latitude = rad(28.5729) # N. Latitude # longitude = rad(80.6490) # W. Longitude # altitude = 0 # Altitude of rocket in meters ecef = pyproj.Proj(proj='geocent', ellps='WGS84', datum='WGS84') lla = pyproj.Proj(proj='latlong', ellps='WGS84', datum='WGS84') Ex, Ey, Ez = pyproj.transform(lla, ecef, longitude, latitude, altitude, radians=True) Evx, Evy, Evz = 0, 0, 0 r_initial = (Ex**2 + Ey**2 + Ez**2)**0.5 #print(Ex, Ey, Ez, r_initial, sep="\t") ## Rocket specs roc_mass = 0.0472 # mass of rocket in kg (not including engine theta = 45 # Angle of launch from z phi = 45 # Angle of launch from x # Original code: eng_file = "C6.csv" # engine file location eng_mass_initial = 0.024 # Loaded engine mass in kg eng_mass_final = 0.0132 # empty engine mass in kg total_mass = roc_mass + eng_mass_initial final_roc_mass = roc_mass + eng_mass_final ## Earth rotation speed at inital position ## Position Velocity Attitude(Earth Centric x-forward y-right z-up) ## latitude and longitude position # Adapted from the orignal code (minor modifications). thrust = np.asarray([[0.0, 0.0], [0.946, 0.031], [4.826, 0.092], [9.936, 0.139], [14.09, 0.192], [11.446, 0.209], [7.381, 0.231], [6.151, 0.248], [5.489, 0.292], [4.921, 0.37], [4.448, 0.475], [4.258, 0.671], [4.542, 0.702], [4.164, 0.723], [4.448, 0.85], [4.353, 1.063], [4.353, 1.211], [4.069, 1.242], [4.258, 1.303], [4.353, 1.468], [4.448, 1.656], [4.448, 1.821], [2.933, 1.834], [1.325, 1.847], [0.0, 1.86]]) thrust_list = np.asarray( [thrust[int(i)][0] for i in range(len(thrust) - 1)]) thrust_time_list = np.asarray( [thrust[i + 1][1] - thrust[i][1] for i in range(0, len(thrust) - 1)]) total_thrust = np.sum(np.multiply(thrust_list, thrust_time_list)) # We moodify the thrust while preserving the sum (this is an adaptation to Nevergrad). # 1: we modify. thrust_list = np.multiply(thrust_list, np.exp(thrust_bias)) # 2: we normalize. thrust_list = thrust_list * total_thrust / np.sum( np.multiply(thrust_list, thrust_time_list)) for i in range(len(thrust) - 1): thrust[i][0] = thrust_list[i] # total_mass vs time curve # this is used to represent the mass loss while the rocket burns fuel mass_time = [] #total_thrust = 0 #for row in thrust: # total_thrust += row[0] mass_loss = eng_mass_initial - eng_mass_final mass_reman = eng_mass_initial for row in thrust: # Equation below weird to me: this is not normalized by time ? the mass which is lost should be proportional # to thrust x delta-time, right ? #percentage = row[0] / total_thrust # percentage of total thrust to find percentage of mass lost percentage = row[0] / np.sum( thrust_list ) # percentage of total thrust to find percentage of mass lost assert percentage >= 0. assert percentage <= 1. mass_loss = mass_reman * percentage mass_reman -= mass_loss total_mass = roc_mass + mass_reman mass_time.append([total_mass, row[1]]) mass_list = [mass_time[i][0] for i in range(0, len(thrust))] # Lists used to store data and later import data to excel Ex_list, Ey_list, Ez_list = np.asarray([Ex]), np.asarray([Ey]), np.asarray( [Ez]) Evx_list, Evy_list, Evz_list = np.asarray([Evx]), np.asarray( [Evy]), np.asarray([Evz]) time_list = np.asarray([0]) r_list = np.asarray([(Ex**2 + Ey**2 + Ez**2)**0.5]) # Initializing variables time = 0. # time in seconds # while thrust is greater than zero # this is while the rocket engine is firing for i in range(len(thrust) - 2): r = (Ex**2 + Ey**2 + Ez**2)**0.5 dt = thrust[i][1] Efx, Efy, Efz = net_force(Ex, Ey, Ez, Evx, Evy, Evz, mass_list[i]) Ex += (Evx * dt) Ey += (Evy * dt) Ez += (Evz * dt) dt = thrust[i + 1][1] - thrust[i][1] Evz += ((((thrust[i][0] * math.cos(theta)) + Efz) * dt) / mass_list[i]) Evx += (((thrust[i][0] * math.sin(theta) * math.cos(phi)) + Efx) * dt) / mass_list[i] Evy += ((( (thrust[i][0] * math.sin(theta) * math.sin(phi)) + Efy) * dt) / mass_list[i]) time += dt Ex_list = np.append(Ex_list, (round(Ex, 6))) Ey_list = np.append(Ey_list, (round(Ey, 6))) Ez_list = np.append(Ez_list, (round(Ez, 6))) Evx_list = np.append(Evx_list, (round(Evx, 6))) Evy_list = np.append(Evy_list, (round(Evy, 6))) Evz_list = np.append(Evz_list, (round(Evz, 6))) time_list = np.append(time_list, (round(time, 6))) r_list = np.append(r_list, (round(r, 6))) # After thrust # This is when the engine is out of fuel and there is no longer a thrust force time_step = .05 # time time_step in seconds dt = time_step while r > r_initial: r = (Ex**2 + Ey**2 + Ez**2)**0.5 Efx, Efy, Efz = net_force(Ex, Ey, Ez, Evx, Evy, Evz, final_roc_mass) Ex += (Evx * dt) Ey += (Evy * dt) Ez += (Evz * dt) Evx += ((Efx * dt) / final_roc_mass) Evy += ((Efy * dt) / final_roc_mass) Evz += ((Efz * dt) / final_roc_mass) #print(Evx, Evy, Evz, r, sep='\t') time += dt #update(Ex, Ey, Ez, Evx, Evy, Evz, time, r) Ex_list = np.append(Ex_list, (round(Ex, 6))) Ey_list = np.append(Ey_list, (round(Ey, 6))) Ez_list = np.append(Ez_list, (round(Ez, 6))) Evx_list = np.append(Evx_list, (round(Evx, 6))) Evy_list = np.append(Evy_list, (round(Evy, 6))) Evz_list = np.append(Evz_list, (round(Evz, 6))) time_list = np.append(time_list, (round(time, 6))) r_list = np.append(r_list, (round(r, 6))) return 1.0 - max( Ez_list) / 3032708.353202 # Should be 0 for input (0.,....,0.)
# Reproject to 3857 # Necessary because original intersection extraction had null projection print "reprojecting raw intersection shapefile" inters = fiona.open(inters_shp_path_raw) inproj = pyproj.Proj(init='epsg:4326') outproj = pyproj.Proj(init='epsg:3857') with fiona.open(inters_shp_path, 'w', crs=from_epsg(3857), schema=inters.schema, driver='ESRI Shapefile') as output: for inter in inters: coords = inter['geometry']['coordinates'] re_point = pyproj.transform(inproj, outproj, coords[0], coords[1]) point = Point(re_point) output.write({ 'geometry': mapping(point), 'properties': inter['properties'] }) # Read in boston segments + mass DOT join roads_shp_path = MAP_FP + '/ma_cob_spatially_joined_streets.shp' roads = [(shape(road['geometry']), road['properties']) for road in fiona.open(roads_shp_path)] print "read in {} road segments".format(len(roads)) # unique id did not get included in shapfile, need to add it for adjacency for i, road in enumerate(roads): road[1]['orig_id'] = int(str(99) + str(i))
def trans_egsa_wgs(x, y, z): wx, wy, wz = transform(egsa, wgs, x, y, z) return wx, wy, wz
def trans_wgs_egsa(x, y, z): ex, ey, ez = transform(wgs, egsa, x, y, z) return ex, ey, ez def trans_egsa_wgs(x, y, z): wx, wy, wz = transform(egsa, wgs, x, y, z) return wx, wy, wz def dm_to_decimal(f): moires, lepta = str(f).split('.') lepta = int(int(lepta) * 100 / 60) res = moires + '.' + str(lepta) return float(res) # a = Proj('+proj=latlong +ellps=GRS80 +towgs84=-199.87,74.79,246.62') astlon = 23.7163375 alat = dm_to_decimal(39.39) alon = dm_to_decimal(-1.57) blat = dm_to_decimal(39.45) blon = dm_to_decimal(-1.45) # Bessel params.... check + and - (probably inverted) # towgs84='456.39,372.62,496.82,-12.664e-6,-5.620e-6,-10.854e-6,15.9e-6' a = Proj(proj='aeqd',ellps='bessel',pm='athens',lon_0=alon,lat_0=alat) b = Proj(proj='aeqd',ellps='bessel',pm='athens',lon_0=blon,lat_0=blat) x, y, z = transform(a, b, -1500, -5000, 0) print(x, y)
def coordConvert(x, y): return transform(inProj, outProj, x, y)
def main(file): # Access global variable global k_iter # Determine if the file is empty if os.stat(file).st_size == 0: return # Read CSV file data = pd.read_csv(file, engine="c", header=None, delim_whitespace=True) # Convert to numpy array and floats data = np.float64(pd.DataFrame.as_matrix(data)) # Wrap longitude to -180 to 180 degrees data[:, 1] = wrapTo180(data[:, 1]) # Get quality flag - keep valid records I_flag = (data[:, 12] == 0) & (data[:, 15] == 0) # Only keep valid records data = data[I_flag, :] # If mask avaliable if fmask != 'None': # Reproject coordinates (x, y) = pyproj.transform(projGeo, projGrd, data[:, 1], data[:, 0]) # Interpolation of grid to points for masking Ii = bilinear2d(Xm, Ym, Zm, x.T, y.T, order=1) # Set all NaN's to zero Ii[np.isnan(Ii)] = 0 # Convert to boolean Im = Ii == 1 # Keep only data inside mask data = data[Im, :] # If file is empty - skip file if len(data) == 0: return # Load satellite parameters cyc = data[:, 3] # Cycle number rel = data[:, 4] # Relative orbit lat = data[:, 0] # Latitude (deg) lon = data[:, 1] # Longitude (deg) t_sec = data[:, 2] # Time (secs since 2000) r_ice1 = data[:,13] * 1e-3 # Range ICE-1 retracker (m) a_sat = data[:, 5] * 1e-3 # Altitude of satellite (m) bs_ice1 = data[:,14] * 1e-2 # Backscatter of ICE-1 retracker (dB) lew_ice2 = data[:,19] * 1e-3 # Leading edge width from ICE-2 (m) tes_ice2 = data[:,20] # Trailing edge slope fram ICE-2 (1/m) #orb_type = data[:,22] # Orbit type A(0)/D(1) # Load geophysical corrections - (mm -> m) h_ion = data[:, 6] * 1e-3 # Ionospheric cor h_dry = data[:, 7] * 1e-3 # Dry tropo cor h_wet = data[:, 8] * 1e-3 # Wet tropo cor h_geo = data[:, 9] * 1e-3 # Pole tide h_sol = data[:,10] * 1e-3 # Solid tide # Compute correct time - add back year 2000 in secs t_sec += 2000 * 365.25 * 24 * 3600. # Compute time in decimal years t_year = t_sec / (365.25 * 24 * 3600.) # Compute time since 1970 - remove year 1970 in secs t_sec -= 1970 * 365.25 * 24 * 3600. # Sum all corrections h_cor = h_ion + h_dry + h_wet + h_geo + h_sol # Correct range r_ice1_cor = (r_ice1 + h_cor) # Compute surface elevation h_ice1 = a_sat - r_ice1_cor # Separate tracks into asc/des orbits (i_asc, i_des) = track_type(t_sec, lat) # Create orbit number container orb = np.zeros(lat.shape) orb_type = np.zeros(lat.shape) # Set orbit numbers if len(lat[i_asc]) > 0: # Create independent track references orbit_num = np.char.add(str(index), str(k_iter)).astype('int') # Set vector to number orb[i_asc] = orbit_num # Set orbit type orb_type[i_asc] = 0 # Increase counter k_iter += 1 # Set orbit numbers if len(lat[i_des]) > 0: # Create independent track references orbit_num = np.char.add(str(index), str(k_iter)).astype('int') # Set vector to number orb[i_des] = orbit_num # Set orbit type orb_type[i_des] = 1 # Increase counter k_iter += 1 # Sum all corrections h_cor = h_ion + h_dry + h_wet + h_geo + h_sol # Correct range r_ice1_cor = (r_ice1 + h_cor) # Compute surface elevation h_ice1 = a_sat - r_ice1_cor # Current file iFile = np.column_stack(( orb, lat, lon, t_sec, h_ice1, t_year, bs_ice1, lew_ice2, tes_ice2, r_ice1_cor, h_ion, h_dry, h_wet, h_geo, h_sol, orb_type)) # Create varibales names fields = ['orbit', 'lat', 'lon', 't_sec', 'h_cor', 't_year', 'bs', 'lew', 'tes', 'r_ice1_cor', 'h_ion', 'h_dry', 'h_wet', 'h_geo', 'h_sol', 'orb_type'] # Save ascending file if len(lat[i_asc]) > 0: # Create file ending str_orb = '_READ_A' # Change path/name of read file name, ext = os.path.splitext(os.path.basename(file)) ofile = os.path.join(outdir, name + str_orb + ext) ofile = ofile.replace('.txt', '.h5') # Write to file with h5py.File(ofile, 'w') as f: [f.create_dataset(k, data=d) for k, d in zip(fields, iFile[i_asc].T)] # What file are we reading print ofile # Save descending file if len(lat[i_des]) > 0: # Create file ending str_orb = '_READ_D' # Change path/name of read file name, ext = os.path.splitext(os.path.basename(file)) ofile = os.path.join(outdir, name + str_orb + ext) ofile = ofile.replace('.txt', '.h5') # Write to file with h5py.File(ofile, 'w') as f: [f.create_dataset(k, data=d) for k, d in zip(fields, iFile[i_des].T)] # What file are we reading print ofile
def execute(self, stream, process_limits=None, origin_resource=None, channels=None, **kwargs): ''' Execute the stack node. Parameters ---------- stream : :class:`obspy.core.Stream` The data to process. ''' # Check for needed keyword arguments. if not self.kwargs_exists(['event'], **kwargs): raise RuntimeError( "The needed event argument was not passed to the execute method." ) event = kwargs['event'] # Load the quarry_blast information. blast_filename = self.pref_manager.get_value('blast_file') if os.path.exists(blast_filename): with open(blast_filename, 'r') as fp: quarry_blast = json.load( fp=fp, cls=quarry_blast_validation.QuarryFileDecoder) else: raise RuntimeError("Couldn't open the blast file %s.", blast_filename) # Get the baumit quarry blast id from the event tags. id_tag = [x for x in event.tags if x.startswith('baumit_id:')] if id_tag: id_tag = id_tag[0] else: self.logger.error( "No baumit quarry blast id tag found for event %d.", event.db_id) return spec, baumit_id = id_tag.split(':') # Compute the resultant of the stations. res_stream = obspy.core.Stream() res3d_stream = obspy.core.Stream() orig_stream = obspy.core.Stream() res_stations = [] for cur_detection in event.detections: # TODO: Select the detection timespan only. cur_stream = stream.select(network=cur_detection.scnl[2], station=cur_detection.scnl[0], location=cur_detection.scnl[3]) resultant_channels = ['Hnormal', 'Hparallel'] resultant_3d_channels = ['Hnormal', 'Hparallel', 'Z'] # Compute the 2D-resultant used for the magnitude computation. cur_res_stream = self.compute_resultant(cur_stream, resultant_channels) if not cur_res_stream: continue #Compute the 3D-resultant used for reporting of the DUBA stations. cur_res3d_stream = self.compute_resultant(cur_stream, resultant_3d_channels) orig_stream = orig_stream + cur_stream res_stream = res_stream + cur_res_stream res3d_stream = res3d_stream + cur_res3d_stream res_stations.append(cur_detection.channel.parent_station) # Handle the DUBAM station separately. Due to a time-error relative to # DUBA, the DUBAM detections are most likely not included in the event # detections. stat_dubam = [x for x in res_stations if x.name == 'DUBAM'] self.logger.debug( "##################### DUBAM HANDLING ###############") self.logger.debug(stat_dubam) if not stat_dubam: cur_stream = stream.select(network='MSSNet', station='DUBAM', location='00') self.logger.debug(cur_stream) # Compute the 2D-resultant used for the magnitude computation. resultant_channels = ['Hnormal', 'Hparallel'] cur_res_stream = self.compute_resultant(cur_stream, resultant_channels) if cur_res_stream: #Compute the 3D-resultant used for reporting of the DUBA stations. resultant_3d_channels = ['Hnormal', 'Hparallel', 'Z'] cur_res3d_stream = self.compute_resultant( cur_stream, resultant_3d_channels) orig_stream = orig_stream + cur_stream res_stream = res_stream + cur_res_stream res3d_stream = res3d_stream + cur_res3d_stream cur_station = self.project.geometry_inventory.get_station( name='DUBAM')[0] res_stations.append(cur_station) self.logger.debug("res_stations: %s", res_stations) self.logger.debug("res_stream: %s", res_stream) self.logger.debug("res3d_stream: %s", res3d_stream) # Handle possible coordinate changes of station DUBAM. # If the field x_dubam_1 is set, the alternative DUBAM position is # used. stat_dubam = [x for x in res_stations if x.name == 'DUBAM'] if stat_dubam: stat_dubam = stat_dubam[0] if quarry_blast[baumit_id]['x_dubam_1'] is not None: stat_dubam.x = quarry_blast[baumit_id]['x_dubam_1'] stat_dubam.y = quarry_blast[baumit_id]['y_dubam_1'] stat_dubam.z = quarry_blast[baumit_id]['z_dubam_1'] stat_dubam.coord_system = 'epsg:31256' # TODO: Check if all expected stations have got a trigger. Test the # missing stations for available data an eventually missed events. Use # a threshold value of the maximum amplitude as a rule whether to # include the missed station into the PGV computation or not. # Compute the max. PGV. max_pgv = [ (str.join(':', (x.stats.station, x.stats.network, x.stats.location)), np.max(x.data)) for x in res_stream ] max_pgv_3d = [ (str.join(':', (x.stats.station, x.stats.network, x.stats.location)), np.max(x.data)) for x in res3d_stream ] # Compute the magnitude. # TODO: Check the standard for the sign of the station correction. # Brueckl subtracts the station correction. proj_baumit = pyproj.Proj(init='epsg:' + quarry_blast[baumit_id]['epsg']) proj_wgs84 = pyproj.Proj(init='epsg:4326') if res_stations: stat_lonlat = np.array([x.get_lon_lat() for x in res_stations]) stat_x, stat_y = pyproj.transform(proj_wgs84, proj_baumit, stat_lonlat[:, 0], stat_lonlat[:, 1]) stat_z = np.array([x.z for x in res_stations]) quarry_x = quarry_blast[baumit_id]['x'] quarry_y = quarry_blast[baumit_id]['y'] quarry_z = quarry_blast[baumit_id]['z'] hypo_dist = np.sqrt((stat_x - quarry_x)**2 + (stat_y - quarry_y)**2 + (stat_z - quarry_z)**2) stat_corr = np.zeros(len(max_pgv)) magnitude = np.log10([ x[1] * 1000 for x in max_pgv ]) + 1.6 * np.log10(hypo_dist) - 2.074 + stat_corr else: hypo_dist = [] magnitude = [] # Compute the PSD. psd_data = {} for cur_trace in orig_stream: cur_psd_data = self.compute_psd(cur_trace) cur_scnl = util.traceid_to_scnl(cur_trace.id) cur_scnl_string = ':'.join(cur_scnl) psd_data[cur_trace.id] = cur_psd_data # Compute the dominant frequency. dom_frequ = {} dom_stat_frequ = {} for cur_station in res_stations: cur_psd_keys = [ x for x in psd_data.keys() if x.startswith(cur_station.network + '.' + cur_station.name + '.') ] cur_df = [] for cur_key in cur_psd_keys: cur_nfft = psd_data[cur_key]['n_fft'] left_fft = int(np.ceil(cur_nfft / 2.)) max_ind = np.argmax(psd_data[cur_key]['psd'][1:left_fft]) dom_frequ[cur_key] = psd_data[cur_key]['frequ'][max_ind] cur_df.append(dom_frequ[cur_key]) dom_stat_frequ[cur_station.snl_string] = np.mean(cur_df) # Update the quarry blast information dictionary. export_max_pgv = dict(max_pgv) export_magnitude = dict( list(zip([x.snl_string for x in res_stations], magnitude))) quarry_blast[baumit_id]['computed_on'] = utcdatetime.UTCDateTime() quarry_blast[baumit_id]['event_time'] = event.start_time quarry_blast[baumit_id]['max_pgv'] = {} quarry_blast[baumit_id]['max_pgv']['data'] = export_max_pgv quarry_blast[baumit_id]['max_pgv'][ 'used_channels'] = resultant_channels quarry_blast[baumit_id]['max_pgv_3d'] = {} quarry_blast[baumit_id]['max_pgv_3d']['data'] = dict(max_pgv_3d) quarry_blast[baumit_id]['max_pgv_3d'][ 'used_channels'] = resultant_3d_channels quarry_blast[baumit_id]['magnitude'] = {} quarry_blast[baumit_id]['magnitude']['station_mag'] = export_magnitude quarry_blast[baumit_id]['magnitude']['network_mag'] = np.mean( magnitude) quarry_blast[baumit_id]['magnitude']['network_mag_std'] = np.std( magnitude) quarry_blast[baumit_id]['dom_frequ'] = dom_frequ quarry_blast[baumit_id]['dom_stat_frequ'] = dom_stat_frequ quarry_blast[baumit_id]['hypo_dist'] = dict( list(zip([x.snl_string for x in res_stations], hypo_dist))) # Save the quarry blast information. with open(blast_filename, 'w') as fp: json.dump(quarry_blast, fp=fp, cls=quarry_blast_validation.QuarryFileEncoder) # Write a result file for the event. output_dir = self.pref_manager.get_value('report_data_dir') filename = 'blast_report_data_event_%010d.pkl' % event.db_id report_data = {} report_data['baumit_id'] = baumit_id report_data['blast_data'] = quarry_blast[baumit_id] report_data['psd_data'] = psd_data with open(os.path.join(output_dir, filename), 'w') as fp: pickle.dump(report_data, fp) # Clear the computation request flag in the event database. event.tags.remove('mss_result_needed') event.tags.append('mss_result_computed') event.write_to_database(self.project)
def plot(self,alt=np.array([None])): fig, ax = plt.subplots() ax.ticklabel_format(useOffset=False) plt.subplot(141) plt.plot(self.times,self.mu_history[0,:]) plt.title("X vs Time") plt.xlabel("Time [hrs]") plt.ylabel("X [m]") plt.subplot(142) plt.plot(self.times,self.mu_history[1,:]) plt.title("Y vs Time") plt.xlabel("Time [hrs]") plt.ylabel("Y [m]") plt.subplot(143) plt.plot(self.times,self.mu_history[2,:]) plt.title("Z vs Time") plt.xlabel("Time [hrs]") plt.ylabel("Z [m]") plt.subplot(144) plt.plot(self.times,self.mu_history[3,:]) plt.title("Time Bias vs Time") plt.xlabel("Time [hrs]") plt.ylabel("Time Bias [m]") # covariance plot plt.figure() plt.title("Trace of Covariance Matrix vs. Time") plt.xlabel("Time [hrs]") plt.ylabel("Trace") plt.plot(self.times,self.P_history) # trajectory plot lla_traj = np.zeros((len(self.times),3)) if alt.all() == None: lon, lat, alt = pyproj.transform(self.ecef, self.lla, self.mu_history[0,:], self.mu_history[1,:], self.mu_history[2,:], radians=False) else: lon, lat, reject = pyproj.transform(self.ecef, self.lla, self.mu_history[0,:], self.mu_history[1,:], self.mu_history[2,:], radians=False) print("alt shape",alt.shape) lla_traj[:,0] = lat lla_traj[:,1] = lon lla_traj[:,2] = alt fig, ax = plt.subplots() ax.ticklabel_format(useOffset=False) plt.plot(lla_traj[:,1],lla_traj[:,0],label="Our Position Solution") if self.odom_file != None: lat_truth = self.odom_df['GPS(0):Lat[degrees]'].to_numpy() lon_truth = self.odom_df['GPS(0):Long[degrees]'].to_numpy() plt.plot(lon_truth,lat_truth,'g',label="DJI's Position Solution") plt.legend() plt.xlim([-122.1759,-122.1754]) plt.ylim([37.42620,37.42660]) ax.set_yticks([37.4262,37.4263,37.4264,37.4265,37.4266]) plt.xlabel("Longitude [deg]") plt.ylabel("Latitude [deg]") fig = plt.figure() ax = fig.gca(projection='3d') ax.plot(lla_traj[:,1], lla_traj[:,0], lla_traj[:,2], label='our solution') if self.odom_file != None: lat_truth = self.odom_df['GPS(0):Lat[degrees]'].to_numpy()[self.truth_indexes] lon_truth = self.odom_df['GPS(0):Long[degrees]'].to_numpy()[self.truth_indexes] h_truth = self.odom_df['GPS(0):heightMSL[meters]'].to_numpy()[self.truth_indexes] latf = self.odom_df['GPS(0):Lat[degrees]'].values[-1] lonf = self.odom_df['GPS(0):Long[degrees]'].values[-1] hf = h_truth[-1] lat0 = self.odom_df['GPS(0):Lat[degrees]'][0] lon0 = self.odom_df['GPS(0):Long[degrees]'][0] h0 = h_truth[0] plt.plot([lon0],[lat0],[h0],'go') plt.plot([lonf],[latf],[hf],'ro') plt.plot(lon_truth,lat_truth,h_truth,'g',label="DJI's Position Solution") ax.legend() ax.ticklabel_format(useOffset=False) ax.set_xlim([-122.1759,-122.1754]) ax.set_ylim([37.42620,37.42660]) ax.set_zlim([0.,2.5]) ax.set_xlabel('\n\n Longitude [deg]') ax.set_ylabel('Latitude [deg]') ax.set_zlabel('Altitude [m]') ax.set_xticks([-122.1759, -122.17565, -122.1754]) ax.set_yticks([37.42630,37.42640,37.42650]) ax.view_init(elev=10., azim=20.) if self.odom_file != None: steps = np.arange(len(lat_truth)) lat_error = np.abs(lat_truth-lla_traj[:,0]) lon_error = np.abs(lon_truth-lla_traj[:,1]) h_error = np.abs(h_truth-lla_traj[:,2]) print("lat avg: ",np.mean(lat_error)) print("lon avg: ",np.mean(lon_error)) print("h avg: ",np.mean(h_error)) plt.figure() plt.subplot(131) plt.title("Latitude Error [degrees latitude]") plt.ylabel("Latitude Error [degrees latitude]") plt.xlabel("Time Step") plt.ticklabel_format(axis="y", style="sci", scilimits=(0,0)) plt.plot(steps,lat_error) plt.subplot(132) plt.xlabel("Time Step") plt.ticklabel_format(axis="y", style="sci", scilimits=(0,0)) plt.title("Longitude Error [degrees longitude]") plt.ylabel("Longitude Error [degrees longitude]") plt.plot(steps,lon_error) plt.subplot(133) plt.xlabel("Time Step") plt.title("Altitude Error [m]") plt.ylabel("Altitude Error [m]") plt.plot(steps,h_error) # save to file df_traj = pd.DataFrame() df_traj['latitude'] = lla_traj[:,0] df_traj['longitude'] = lla_traj[:,1] df_traj['elevation'] = lla_traj[:,2] df_traj.to_csv('./data/calculated_trajectory.csv',index=False) plt.show()
def latlon_to_xy(lat, lon, base_crs=albers_conus_crs()): p1 = Proj(base_crs) p2 = Proj(proj='latlong', datum='WGS84') x, y = transform(p2, p1, np.asarray(lon), np.asarray(lat)) return x, y
def save(self, calculator): self.generatePointsLayer(calculator) self.generatePolygonsLayer(calculator) # 转换成投影坐标 srcCRS = pyproj.Proj(proj='latlon', datum='WGS84') # 定义数据地理坐标系 targetCRS = pyproj.Proj(proj='merc', datum='WGS84') # 定义转换投影坐标系 for i in range(len(self.x)): #x1, y1 = srcCRS(self.x[i], self.y[i]) x2, y2 = pyproj.transform(srcCRS, targetCRS, self.x[i], self.y[i]) self.x[i] = x2 self.y[i] = y2 #print(x2,y2, self.z[i]) points = [(self.x[i], self.y[i], self.z[i]) for i in range(len(self.x))] # face_data = np.array([([0, pIndex, pIndex + 1], 255, 255, 255), # ([0, pIndex, pIndex + 3], 255, 0, 0), # ], # dtype=[('vertex_indices', 'i4', (3,)), # ('red', 'u1'), ('green', 'u1'), # ('blue', 'u1')]) totalStations = 0 for line in calculator.points: totalStations = totalStations + len(line) vetexCount = len(self.x) edgeCount = totalStations * 4 faceCount = totalStations * 4 rectCount = totalStations print("total station:", totalStations) print("The vetex count:", vetexCount) print("The edge count:", edgeCount) print("The tri face count:", faceCount) print("The rect face count:", rectCount) vertex = np.array(points, dtype=[('x', 'f8'), ('y', 'f8'), ('z', 'f8')]) print("vertex array shape:", vertex.shape) with open(self.filename, 'w') as file: file.write("""ply format ascii 1.0 element vertex {} comment vertices property double x property double y property double z property uint8 red property uint8 green property uint8 blue element edge {} property int32 vertex1 property int32 vertex2 property uint8 red property uint8 green property uint8 blue element face {} property list uchar int vertex_indices end_header """.format(vetexCount, edgeCount, faceCount + rectCount)) # write vexex colors = np.zeros((totalStations, 3), dtype=np.uint8) for i in range(len(self.x)): if i < totalStations: colors[i, :] = random.randint(0, 255), random.randint( 0, 255), random.randint(0, 255) file.write("{} {} {} {} {} {}\n".format( self.x[i], self.y[i], self.z[i], *colors[i, :])) file.flush() else: index = int((i - totalStations) / 4) #print("对应的摄站点为:",index) file.write("{} {} {} {} {} {}\n".format( self.x[i], self.y[i], self.z[i], *colors[index, :])) file.flush() # write edge for i in range(totalStations): pIndex = totalStations + i * 4 file.write("{} {} 0 255 0\n".format(i, pIndex)) file.flush() file.write("{} {} 0 255 0\n".format(i, pIndex + 1)) file.flush() file.write("{} {} 0 255 0\n".format(i, pIndex + 2)) file.flush() file.write("{} {} 0 255 0\n".format(i, pIndex + 3)) file.flush() # write face for i in range(totalStations): pIndex = totalStations + i * 4 #print("Current station:{}, pIndex:{}".format(i,pIndex)) f1 = (i, pIndex, pIndex + 1) f2 = (i, pIndex, pIndex + 3) f3 = (i, pIndex + 3, pIndex + 2) f4 = (i, pIndex + 2, pIndex + 1) #print("conic:", i, f1,f2,f3,f4) file.write("3 {} {} {}\n".format(f1[0], f1[1], f1[2])) file.flush() file.write("3 {} {} {}\n".format(f2[0], f2[1], f2[2])) file.flush() file.write("3 {} {} {}\n".format(f3[0], f3[1], f3[2])) file.flush() file.write("3 {} {} {}\n".format(f4[0], f4[1], f4[2])) file.flush() # write rect for i in range(totalStations): pIndex = totalStations + i * 4 file.write("4 {} {} {} {}\n".format(pIndex, pIndex + 1, pIndex + 2, pIndex + 3)) file.flush() return True
xmax = nodes[triangles[k, 2], 0] ymin = nodes[triangles[k, 0], 1] ymax = nodes[triangles[k, 2], 1] if ((xmin > x0hole) & (xmax < x1hole)) & ((ymin > y0hole) & (ymax < y1hole)): solid_id[k] = 1 else: solid_id[k] = 0 print('done tagging hole') nsolid = int(max(solid_id)) if args.proj != '': print('projecting the node coordinates') x0, y0, z0 = pyproj.transform(lla, myproj, nodes[:, 0], nodes[:, 1], 1e3 * nodes[:, 2], radians=False) nodes[:, 0] = x0 nodes[:, 1] = y0 nodes[:, 2] = z0 print(nodes) print('done projecting') del x0, y0, z0 _, ext = os.path.splitext(args.output_file) if ext == '.ts': fout = open(args.output_file, 'w') for sid in range(nsolid + 1): fout.write("GOCAD TSURF 1\nHEADER {\nname:" + args.objectname +
def __init__(self,sat_file,odom_file=None): # read in data files as dataframe self.sat_df = pd.read_csv(sat_file, index_col=0) self.odom_file = odom_file if odom_file != None: self.odom_df = pd.read_csv(odom_file, index_col=0) # initial lat and lon from dji data lat0 = np.mean(self.odom_df['GPS(0):Lat[degrees]'][0]) lon0 = np.mean(self.odom_df['GPS(0):Long[degrees]'][0]) h0 = 0.0 # convert lat lon to ecef frame self.lla = pyproj.Proj(proj='latlong', ellps='WGS84', datum='WGS84') self.ecef = pyproj.Proj(proj='geocent', ellps='WGS84', datum='WGS84') x0, y0, z0 = pyproj.transform(self.lla, self.ecef, lon0, lat0, h0 , radians=False) # concatenate possible time steps from each data file self.times = np.concatenate((self.odom_df['seconds of week [s]'].to_numpy(),self.sat_df['seconds of week [s]'].to_numpy())) # sort timesteps and force unique self.times = np.sort(np.unique(self.times)) self.initialized_odom = False def find_nearest(array, value): array = np.asarray(array) idx = (np.abs(array - value)).argmin() return idx # indexes for plotting truth later self.truth_indexes = [] for ii in range(len(self.times)): ix = find_nearest(self.odom_df['seconds of week [s]'].to_numpy(),self.times[ii]) self.truth_indexes.append(ix) else: # set initial positions x0 = 0. y0 = 0. z0 = 0. # initialize times self.times = self.sat_df['seconds of week [s]'].to_numpy() # initialize state vector [ x, y, z ] self.mu = np.array([[x0,y0,z0,0.0]]).T self.mu_n = self.mu.shape[0] self.mu_history = self.mu.copy() # initialize covariance matrix self.P = np.eye(self.mu_n)*10E2 self.P_history = [np.trace(self.P)] if odom_file != None: t0 = self.sat_df['seconds of week [s]'].to_numpy()[0] x_calc = [x0,y0,z0] bu_calc = 0.0 input = self.sat_df[self.sat_df['seconds of week [s]'] == t0] for ii in range(20): x_calc, bu_calc = self.least_squares(x_calc,bu_calc,input) self.mu[3][0] = bu_calc if odom_file != None and 'pr [m]' in self.sat_df.columns: # only use the best satellites self.cutoff_angle = 20.0 self.check_data(self.mu,lat0,lon0,False)
def generate_ows_link(self, endpoint, service_type, service_version): if service_version in ("1.1.0", "1.1"): service_version = "1.1.0" elif service_version in ("2.0.0", "2", "2.0"): service_version = "2.0.0" elif service_version in ("1", "1.0", "1.0.0"): service_version = "1.0.0" endpoint = endpoint.strip() original_endpoint = endpoint #parse endpoint's parameters endpoint = endpoint.split("?", 1) endpoint, endpoint_parameters = ( endpoint[0], endpoint[1]) if len(endpoint) == 2 else (endpoint[0], None) endpoint_parameters = endpoint_parameters.split( "&") if endpoint_parameters else None endpoint_parameters = dict( [(p.split("=", 1)[0].upper(), p.split("=", 1)) for p in endpoint_parameters] if endpoint_parameters else []) #get target_crs target_crs = None if service_type == "WFS": target_crs = [ endpoint_parameters.get(k)[1] for k in ["SRSNAME"] if k in endpoint_parameters ] elif service_type in ["WMS", "GWC"]: target_crs = [ endpoint_parameters.get(k)[1] for k in ["SRS", "CRS"] if k in endpoint_parameters ] if target_crs: target_crs = target_crs[0].upper() else: target_crs = self.crs.upper() if self.crs else None #transform the bbox between coordinate systems, if required bbox = self.bbox or [] if bbox: if target_crs != self.crs: try: if self.crs.upper() in epsg_extra: p1 = pyproj.Proj(epsg_extra[self.crs.upper()]) else: p1 = pyproj.Proj(init=self.crs) if target_crs in epsg_extra: p2 = pyproj.Proj(epsg_extra[target_crs]) else: p2 = pyproj.Proj(init=target_crs) bbox[0], bbox[1] = pyproj.transform( p1, p2, bbox[0], bbox[1]) bbox[2], bbox[3] = pyproj.transform( p1, p2, bbox[2], bbox[3]) except Exception as e: raise ValidationError( "Transform the bbox of layer({0}) from crs({1}) to crs({2}) failed.{3}" .format(self.identifier, self.crs, target_crs, str(e))) if service_type == "WFS": #to limit the returned features, shrink the original bbox to 10 percent percent = 0.1 shrinked_min = lambda min, max: (max - min) / 2 - ( max - min) * percent / 2 shrinked_max = lambda min, max: (max - min) / 2 + ( max - min) * percent / 2 shrinked_bbox = [ shrinked_min(bbox[0], bbox[2]), shrinked_min(bbox[1], bbox[3]), shrinked_max(bbox[0], bbox[2]), shrinked_max(bbox[1], bbox[3]) ] else: shrinked_bbox = None bbox2str = lambda bbox, service, version: ', '.join( str(c) for c in bbox ) if service != "WFS" or version == "1.0.0" else ", ".join( [str(c) for c in [bbox[1], bbox[0], bbox[3], bbox[2]]]) if service_type == "WFS": kvp = { "SERVICE": "WFS", "REQUEST": "GetFeature", "VERSION": service_version, } parameters = {} if self.crs: kvp["SRSNAME"] = self.crs.upper() if target_crs: parameters["crs"] = target_crs is_geoserver = endpoint.find("geoserver") >= 0 if service_version == "1.1.0": if is_geoserver: kvp["maxFeatures"] = 20 elif shrinked_bbox: kvp["BBOX"] = bbox2str(shrinked_bbox, service_type, service_version) kvp["TYPENAME"] = self.identifier elif service_version == "2.0.0": if is_geoserver: kvp["count"] = 20 elif shrinked_bbox: kvp["BBOX"] = bbox2str(shrinked_bbox, service_type, service_version) kvp["TYPENAMES"] = self.identifier else: if shrinked_bbox: kvp["BBOX"] = bbox2str(shrinked_bbox, service_type, service_version) kvp["TYPENAME"] = self.identifier elif service_type == "WMS": kvp = { "SERVICE": "WMS", "REQUEST": "GetMap", "VERSION": service_version, "LAYERS": self.identifier, ("SRS", "CRS"): self.crs.upper(), "WIDTH": self.width, "HEIGHT": self.height, "FORMAT": "image/png" } parameters = { "crs": target_crs, "format": endpoint_parameters["FORMAT"][1] if "FORMAT" in endpoint_parameters else kvp["FORMAT"], } if bbox: kvp["BBOX"] = bbox2str(bbox, service_type, service_version) elif service_type == "GWC": service_type = "WMS" kvp = { "SERVICE": "WMS", "REQUEST": "GetMap", "VERSION": service_version, "LAYERS": self.identifier, ("SRS", "CRS"): self.crs.upper(), "WIDTH": 1024, "HEIGHT": 1024, "FORMAT": "image/png" } parameters = { "crs": target_crs, "format": endpoint_parameters["FORMAT"][1] if "FORMAT" in endpoint_parameters else kvp["FORMAT"], "width": endpoint_parameters["WIDTH"][1] if "WIDTH" in endpoint_parameters else kvp["WIDTH"], "height": endpoint_parameters["HEIGHT"][1] if "HEIGHT" in endpoint_parameters else kvp["HEIGHT"], } if not bbox: #bbox is null, use australian bbox bbox = [108.0000, -45.0000, 155.0000, -10.0000] p1 = pyproj.Proj(init="EPSG:4326") p2 = pyproj.Proj(init=target_crs) bbox[0], bbox[1] = pyproj.transform(p1, p2, bbox[0], bbox[1]) bbox[2], bbox[3] = pyproj.transform(p1, p2, bbox[2], bbox[3]) if not hasattr(PreviewTile, target_crs.replace(":", "_")): raise Exception( "GWC service don't support crs({}) ".format(target_crs)) tile_bbox = getattr(PreviewTile, target_crs.replace(":", "_"))(bbox) kvp["BBOX"] = bbox2str(tile_bbox, service_type, service_version) else: raise Exception("Unknown service type({})".format(service_type)) is_exist = lambda k: any([ n.upper() in endpoint_parameters for n in (k if isinstance(k, tuple) or isinstance(k, list) else [k]) ]) querystring = "&".join([ "{}={}".format( k[0] if isinstance(k, tuple) or isinstance(k, list) else k, v) for k, v in kvp.items() if not is_exist(k) ]) if querystring: if original_endpoint[-1] in ("?", "&"): link = "{}{}".format(original_endpoint, querystring) elif '?' in original_endpoint: link = "{}&{}".format(original_endpoint, querystring) else: link = "{}?{}".format(original_endpoint, querystring) else: link = original_endpoint #get the endpoint after removing ows related parameters if endpoint_parameters: is_exist = lambda k: any([ any([k == key.upper() for key in item_key]) if isinstance(item_key, tuple) or isinstance(item_key, list) else k == item_key.upper() for item_key in kvp ]) endpoint_querystring = "&".join([ "{}={}".format(*v) for k, v in endpoint_parameters.items() if not is_exist(k) ]) if endpoint_querystring: endpoint = "{}?{}".format(endpoint, endpoint_querystring) #schema = '{{"protocol":"OGC:{0}", "linkage":"{1}", "version":"{2}"}}'.format(service_type.upper(), endpoint, service_version) schema = { "protocol": "OGC:{}".format(service_type.upper()), "linkage": endpoint, "version": service_version, } schema.update(parameters) return 'None\tNone\t{0}\t{1}'.format(json.dumps(schema), link)
print("MicMac: XifGps2Txt > Extract GPS data from EXIF") subprocess.call("mm3d XifGps2Txt '.*[JPG|jpg]' > img2ortho.log", shell=True) # WGS84 proj to local wgs = Proj(init='epsg:4326') local = Proj(init='epsg:2154') # get lat/lon from micmac generated txt file with open('GpsCoordinatesFromExif.txt') as gpscsv: gpsdata = csv.reader(gpscsv, delimiter=' ') print("Reproject to local X/Y") with open('LocalCoordinatesFromExif.txt', mode='w') as localcsv: localdata = csv.writer(localcsv, delimiter=' ') localdata.writerow(['#F=N', 'X', 'Y', 'Z']) for gps in gpsdata: x2, y2 = transform(wgs, local, gps[1], gps[2]) localdata.writerow([gps[0], x2, y2, gps[3]]) print("Micmac: OriConvert") subprocess.call( 'mm3d OriConvert OriTxtInFile LocalCoordinatesFromExif.txt gps NameCple=FileImagesNeighbour.xml >> img2ortho.log', shell=True) print("Micmac: Tapioca > search for similarities between images") subprocess.call( 'mm3d Tapioca MulScale .*.JPG 300 1200 ExpTxt=0 >> img2ortho.log', shell=True) print("Micmac: Tapas > find relative position/orientation between images") subprocess.call( 'mm3d Tapas RadialBasic .*.JPG Out=Arbitrary ExpTxt=0 >> img2ortho.log', shell=True)
def jtsk2wgs(coords): return pyproj.transform(jtsk, wgs, coords[0], coords[1])
def statistic(request): if request.GET.items() != []: req = str(request.GET.items()[0]) search = re.findall('[\d]+\.[\d]+', str(req)) if len(search) > 7: try: tuple_of_coordinates = [] print search inProj = Proj(init='epsg:3857') outProj = Proj(init='epsg:4326') for i in range(0, len(search) - 1, 2): print 123 tuple_of_coordinates.append( (transform(inProj, outProj, float(search[i]), float(search[i + 1])))) queryset = Finalelim.objects.filter( geom__contained=(Polygon(tuple(tuple_of_coordinates)))) pl = sum( map(lambda i: queryset[i].area_ha, range(len(queryset)))) type_fields = map(lambda i: queryset[i].comment, range(len(queryset))) print type_fields[0] cnt_crop = Counter(type_fields) k = cnt_crop[u'кукурудза'] sn = cnt_crop[u'соняшник'] sy = cnt_crop[u'соя'] ins = cnt_crop[u'інші культури'] k_q = queryset.filter(comment="кукурудза") sn_q = queryset.filter(comment="соняшник") sy_q = queryset.filter(comment="соя") ins_q = queryset.filter(comment="інші культури") if k > 0: pl_k = sum(map(lambda i: k_q[i].area_ha, range(len(k_q)))) else: pl_k = 0 if sn > 0: pl_sn = sum( map(lambda i: sn_q[i].area_ha, range(len(sn_q)))) else: pl_sn = 0 if sy > 0: pl_sy = sum( map(lambda i: sy_q[i].area_ha, range(len(sy_q)))) else: pl_sy = 0 if ins > 0: pl_ins = sum( map(lambda i: ins_q[i].area_ha, range(len(ins_q)))) else: pl_ins = 0 count = queryset.count() if count > 0: return render_to_response( "statistic.html", { "count": queryset.count(), "soya_ha": pl_sy, "soya_cnt": sy, "son_ha": pl_sn, "son_cnt": sn, "k_ha": pl_k, "k_cnt": k, "in_ha": pl_ins, "in_cnt": ins, "pl": pl }) except: return render_to_response("statistic.html", { "count": '0', "pl": '0' })
def convert_from_EPSG4326(xy, dataset): """given a list of (lat, lon) encoded in EPSG:4326, returns a list of (x, y) encoded in dataset.crs""" inproj, outproj = Proj('EPSG:4326'), dataset.crs return list(zip(*transform(inproj, outproj, *zip(*xy))))
def wgs2jtsk(coords): return pyproj.transform(wgs, jtsk, coords[0], coords[1])
# Read data from differetn file formats if ifile.endswith('.npy'): # Load Numpy binary file to memory Points = np.load(ifile) else: # Load ASCII file to memory Points = pd.read_csv(ifile, engine="c", header=None, delim_whitespace=True) # Convert to numpy array Points = pd.DataFrame.as_matrix(Points) # Convert into stenographic coordinates (xp, yp) = pyproj.transform(projGeo, projGrd, Points[:, cx], Points[:, cy]) # Test for different types of input if len(bbox) == 6: # Extract bounding box elements (xmin, xmax, ymin, ymax) = bbox else: # Create bounding box limits (xmin, xmax, ymin, ymax) = (xp.min() - 10.0 * dx), (xp.max() + 10.0 * dx), (yp.min() - 10.0 * dy), (yp.max() + 10.0 * dy)
def center_to_scene_boundaries(center_lat, center_lon, previous_center_lat, previous_center_lon): """ Given a ll center coordinate generate a polygon geometry representing the scenes boundaries Input: center_lat, center_lon: scene center, degrees previous_center_lat, previous_center_lon: previous scene center, degrees """ i_proj = Proj('+init=EPSG:4326') # Choose appropriate UTM projection for plain coordinates w_proj = Proj(longitude_to_utm_epsg(center_lon)) center_ll = (center_lon, center_lat, 0) previous_center_ll = (previous_center_lon, previous_center_lat, 0) center_plain = transform(i_proj, w_proj, *center_ll) previous_center_plain = transform(i_proj, w_proj, *previous_center_ll) # Compute orbit inclination, which will be the # value used for image rotation rotation = math.degrees(math.atan((center_plain[1] - previous_center_plain[1])/ (center_plain[0] - previous_center_plain[0]))) #print center_plain # Half scene in meters hm = 20 * 2906 plain = dict() plain['ul'] = (center_plain[0] - hm, center_plain[1] + hm, 0) plain['ur'] = (center_plain[0] + hm, center_plain[1] + hm, 0) plain['lr'] = (center_plain[0] + hm, center_plain[1] - hm, 0) plain['ll'] = (center_plain[0] - hm, center_plain[1] - hm, 0) #print plain # Generate and rotate plain polygon plain_pol = geometry.Polygon([plain['ul'][:2],plain['ur'][:2],plain['lr'][:2],plain['ll'][:2]]) plain_pol = rotate(plain_pol, angle=rotation, origin=center_plain[:2], use_radians=False) x, y = plain_pol.exterior.coords.xy # Plain to ll llc = dict() llc['ul'] = transform(w_proj, i_proj, x[0], y[0], 0) llc['ur'] = transform(w_proj, i_proj, x[1], y[1], 0) llc['lr'] = transform(w_proj, i_proj, x[2], y[2], 0) llc['ll'] = transform(w_proj, i_proj, x[3], y[3], 0) # Adjust for dateline crossing ref_lon = llc['ul'][0] dateline_crossing = False for coord in llc: if abs(llc[coord][0] - ref_lon) > 10.: # We are crossing the dateline dateline_crossing = True llc_adjusted = dict() if dateline_crossing: # All longitudes are adjusted to positive values for coord in llc: if llc[coord][0] < 0.: llc_adjusted[coord] = (360. + llc[coord][0], llc[coord][1], llc[coord][2]) else: llc_adjusted[coord] = llc[coord] else: llc_adjusted = llc # Create output geometry pol = geometry.Polygon([llc_adjusted['ul'][:2],llc_adjusted['ur'][:2], llc_adjusted['lr'][:2],llc_adjusted['ll'][:2]]) return pol
def match_satellite_to_insitu(tile_ids, primary_b, matchup_b, parameter_b, tt_b, rt_b, platforms_b, bounding_wkt_b, depth_min_b, depth_max_b): the_time = datetime.now() tile_ids = list(tile_ids) if len(tile_ids) == 0: return [] tile_service = NexusTileService() # Determine the spatial temporal extents of this partition of tiles tiles_bbox = tile_service.get_bounding_box(tile_ids) tiles_min_time = tile_service.get_min_time(tile_ids) tiles_max_time = tile_service.get_max_time(tile_ids) # Increase spatial extents by the radius tolerance matchup_min_lon, matchup_min_lat = add_meters_to_lon_lat( tiles_bbox.bounds[0], tiles_bbox.bounds[1], -1 * rt_b.value) matchup_max_lon, matchup_max_lat = add_meters_to_lon_lat( tiles_bbox.bounds[2], tiles_bbox.bounds[3], rt_b.value) # Don't go outside of the search domain search_min_x, search_min_y, search_max_x, search_max_y = wkt.loads( bounding_wkt_b.value).bounds matchup_min_lon = max(matchup_min_lon, search_min_x) matchup_min_lat = max(matchup_min_lat, search_min_y) matchup_max_lon = min(matchup_max_lon, search_max_x) matchup_max_lat = min(matchup_max_lat, search_max_y) # Find the centroid of the matchup bounding box and initialize the projections matchup_center = box(matchup_min_lon, matchup_min_lat, matchup_max_lon, matchup_max_lat).centroid.coords[0] aeqd_proj = pyproj.Proj(proj='aeqd', lon_0=matchup_center[0], lat_0=matchup_center[1]) lonlat_proj = pyproj.Proj(proj='lonlat') # Increase temporal extents by the time tolerance matchup_min_time = tiles_min_time - tt_b.value matchup_max_time = tiles_max_time + tt_b.value print "%s Time to determine spatial-temporal extents for partition %s to %s" % ( str(datetime.now() - the_time), tile_ids[0], tile_ids[-1]) # Query edge for all points within the spatial-temporal extents of this partition the_time = datetime.now() edge_session = requests.Session() edge_results = [] with edge_session: for insitudata_name in matchup_b.value.split(','): bbox = ','.join([ str(matchup_min_lon), str(matchup_min_lat), str(matchup_max_lon), str(matchup_max_lat) ]) edge_response = query_edge(insitudata_name, parameter_b.value, matchup_min_time, matchup_max_time, bbox, platforms_b.value, depth_min_b.value, depth_max_b.value, session=edge_session) if edge_response['totalResults'] == 0: continue r = edge_response['results'] for p in r: p['source'] = insitudata_name edge_results.extend(r) print "%s Time to call edge for partition %s to %s" % ( str(datetime.now() - the_time), tile_ids[0], tile_ids[-1]) if len(edge_results) == 0: return [] # Convert edge points to utm the_time = datetime.now() matchup_points = np.ndarray((len(edge_results), 2), dtype=np.float32) for n, edge_point in enumerate(edge_results): try: x, y = wkt.loads(edge_point['point']).coords[0] except ReadingError: try: x, y = Point( *[float(c) for c in edge_point['point'].split(' ')]).coords[0] except ValueError: y, x = Point( *[float(c) for c in edge_point['point'].split(',')]).coords[0] matchup_points[n][0], matchup_points[n][1] = pyproj.transform( p1=lonlat_proj, p2=aeqd_proj, x=x, y=y) print "%s Time to convert match points for partition %s to %s" % ( str(datetime.now() - the_time), tile_ids[0], tile_ids[-1]) # Build kdtree from matchup points the_time = datetime.now() m_tree = spatial.cKDTree(matchup_points, leafsize=30) print "%s Time to build matchup tree" % (str(datetime.now() - the_time)) # The actual matching happens in the generator. This is so that we only load 1 tile into memory at a time match_generators = [ match_tile_to_point_generator(tile_service, tile_id, m_tree, edge_results, bounding_wkt_b.value, parameter_b.value, rt_b.value, lonlat_proj, aeqd_proj) for tile_id in tile_ids ] return chain(*match_generators)
def convert_to_EPSG4326(self, xy, dataset): """given a list of (x, y) encoded in dataset.crs, returns a list of (x, y) encoded in EPSG4326""" inproj, outproj = dataset.crs, Proj('EPSG:4326') return list(zip(*transform(inproj, outproj, *zip(*xy))))
def covert_coordinate_from_4326_to_DC(lat, lon): # covert to meter inProj = Proj(init='epsg:4326') outProj = Proj(init='epsg:26985') lon2, lat2 = transform(inProj, outProj, lon, lat) return (lat2, lon2)
def transform(x_coords, y_coords, src_prj=None, dst_prj=None): """Calculate projection coordinates.""" return pyproj.transform(src_prj, dst_prj, x_coords, y_coords)
def xy_to_latlon(image, x, y): east, north = image.xy(x, y) p1 = Proj(image.crs) p2 = Proj(proj=LATLONG, datum=WGS84) lon, lat = transform(p1, p2, east, north) return lat, lon
def covert_coordinate_from_DC_to_4326(lat, lon): outProj = Proj(init='epsg:4326') inProj = Proj(init='epsg:26985') lon2, lat2 = transform(inProj, outProj, lon, lat) return (lat2, lon2)
#except: pass print( "Humm... an empty response. Are you sure about the exact OSM tag for your region ?" ) print("Exiting with no extent created.") del (vector_map) time.sleep(1) sys.exit(0) if epsg_code != '4326': name += '_' + epsg_code print("Changing coordinates to match EPSG code") import pyproj import shapely.ops s_proj = pyproj.Proj(init='epsg:4326') t_proj = pyproj.Proj(init='epsg:' + epsg_code) reprojection = lambda x, y: pyproj.transform(s_proj, t_proj, x, y) multipolygon_area = shapely.ops.transform(reprojection, multipolygon_area) vector_map.encode_MultiPolygon(multipolygon_area, VECT.dummy_alt, 'WATER', check=True, cut=False) vector_map.write_node_file(name + '.node') vector_map.write_poly_file(name + '.poly') print("Triangulate...") MESH.triangulate(name, os.path.join(os.path.dirname(sys.argv[0]), '..')) ((xmin, ymin, xmax, ymax), mask_im) = triangulation_to_image(name, pixel_size, grid_size_or_bbox) print("Mask size : ", mask_im.size, "pixels.")
def xyz2lb(x,y,z): inproj = pyproj.Proj("+proj=geocent +datum=WGS84 +units=m +no_defs") outproj = pyproj.Proj("+proj=longlat +datum=WGS84 +no_defs") val= pyproj.transform(inproj, outproj, x,y,z) return([val[1],val[0]])
def trans_wgs_egsa(x, y, z): ex, ey, ez = transform(wgs, egsa, x, y, z) return ex, ey, ez
def init_from_wgs84_params( cls, sensor_lon_decimal_degrees, # type: float sensor_lat_decimal_degrees, # type: float sensor_altitude, # type: float omega, # type: float phi, # type: float kappa, # type: float npix_x, # type: int npix_y, # type: int pixel_pitch_x, # type: float pixel_pitch_y, # type: float focal_length, # type: float alt_units='meters', # type: str omega_units='radians', # type: str phi_units='radians', # type: str kappa_units='radians', # type: str pixel_pitch_x_units='micrometer', # type: str pixel_pitch_y_units='micrometer', # type: str focal_length_units='mm', # type: str flip_x=False, # type: bool flip_y=False, # type: bool ): # type: (...) -> IdealPinholeFpaLocalUtmPointCalc """ :param sensor_lon_decimal_degrees: :param sensor_lat_decimal_degrees: :param sensor_altitude: :param omega: :param phi: :param kappa: :param npix_x: :param npix_y: :param pixel_pitch_x: :param pixel_pitch_y: :param focal_length: :param alt_units: :param omega_units: :param phi_units: :param kappa_units: :param pixel_pitch_x_units: :param pixel_pitch_y_units: :param focal_length_units: :param flip_x: :param flip_y: :return: For now the altitude must be relative to the DEM used for orthorectification. For example, if the DEM is relative to the geoid, then sensor_altitude must also be relative to the geoid too. """ native_proj = proj_utils.decimal_degrees_to_local_utm_proj( sensor_lon_decimal_degrees, sensor_lat_decimal_degrees) proj_4326 = crs_defs.PROJ_4326 sensor_x_local, sensor_y_local = transform(proj_4326, native_proj, sensor_lon_decimal_degrees, sensor_lat_decimal_degrees) utm_point_calc = IdealPinholeFpaLocalUtmPointCalc() pinhole_camera = PinholeCamera() pinhole_camera.init_pinhole_from_coeffs( sensor_x_local, sensor_y_local, sensor_altitude, omega, phi, kappa, focal_length, x_units='meters', y_units='meters', z_units=alt_units, omega_units=omega_units, phi_units=phi_units, kappa_units=kappa_units, focal_length_units=focal_length_units) utm_point_calc.set_projection(native_proj) pp_x_meters = pixel_pitch_x * ureg.parse_expression( pixel_pitch_x_units) pp_y_meters = pixel_pitch_y * ureg.parse_expression( pixel_pitch_y_units) pp_x_meters = pp_x_meters.to('meters').magnitude pp_y_meters = pp_y_meters.to('meters').magnitude utm_point_calc._pixel_pitch_x_meters = pp_x_meters utm_point_calc._pixel_pitch_y_meters = pp_y_meters utm_point_calc.set_approximate_lon_lat_center(sensor_x_local, sensor_y_local) utm_point_calc._npix_x = npix_x utm_point_calc._npix_y = npix_y utm_point_calc._flip_x = flip_x utm_point_calc._flip_y = flip_y utm_point_calc._pinhole_camera = pinhole_camera return utm_point_calc