def test_bbox_line_string(): ls = LineString([(8.919, 44.4074), (8.923, 44.4075)]) bb = bbox(ls) assert bb[0] == 8.919 assert bb[1] == 44.4074 assert bb[2] == 8.923 assert bb[3] == 44.4075
def test_bbox_point(): p = Point((-75.343, 39.984)) bb = bbox(p) assert bb[0] == -75.343 assert bb[1] == 39.984 assert bb[2] == -75.343 assert bb[3] == 39.984
def test_bbox_multi_point(): mp = MultiPoint([(-155.52, 19.61), (-156.22, 20.74), (-157.97, 21.46)]) bb = bbox(mp) assert bb[0] == -157.97 assert bb[1] == 19.61 assert bb[2] == -155.52 assert bb[3] == 21.46
def test_bbox_polygon(): p = Polygon([[(2.38, 57.322), (23.194, -20.28), (-120.43, 19.15), (2.38, 57.322)]]) bb = bbox(p) assert bb[0] == -120.43 assert bb[1] == -20.28 assert bb[2] == 23.194 assert bb[3] == 57.322
def define_origin(geojson, origin): if not origin: origin = "centroid" if isinstance(origin, list): return get_coord(origin) bb = bbox(geojson) west = bb[0] south = bb[1] east = bb[2] north = bb[3] if (origin == "sw" or origin == "southwest" or origin == "westsouth" or origin == "bottomleft"): return [west, south] elif (origin == "se" or origin == "southeast" or origin == "eastsouth" or origin == "bottomright"): return [east, south] elif (origin == "nw" or origin == "northwest" or origin == "westnorth" or origin == "topleft"): return [west, north] elif (origin == "ne" or origin == "northeast" or origin == "eastnorth" or origin == "topright"): return [east, north] elif origin == "center": return center(geojson)["geometry"]["coordinates"] elif origin is None or origin == "centroid": return centroid(geojson)["geometry"]["coordinates"] else: raise Exception("invalid origin")
def test_bbox_feature(): my_line = LineString([(-152.62, 51.21), (5.21, 10.69)]) f = Feature(geometry=my_line) bb = bbox(f) assert bb[0] == -152.62 assert bb[1] == 10.69 assert bb[2] == 5.21 assert bb[3] == 51.21
def test_bbox_geometry_collection(): my_point = Point((23.532, -63.12)) my_line = LineString([(-152.62, 51.21), (5.21, 10.69)]) geo_collection = GeometryCollection([my_point, my_line]) bb = bbox(geo_collection) assert bb[0] == -152.62 assert bb[1] == -63.12 assert bb[2] == 23.532 assert bb[3] == 51.21
def test_feature_collection(): line1_feature = Feature(geometry=LineString([(8.919, 44.4074), (8.923, 44.4075)])) line2_feature = Feature(geometry=LineString([(-152.62, 51.21), (5.21, 10.69)])) fc = FeatureCollection([line1_feature, line2_feature]) bb = bbox(fc) assert bb[0] == -152.62 assert bb[1] == 10.69 assert bb[2] == 8.923 assert bb[3] == 51.21
def test_bbox_multi_line_string(): mls = MultiLineString(( [(3.75, 9.25), (-130.95, 1.52)], [(23.15, -34.25), (-1.35, -4.65), (3.45, 77.95)], )) bb = bbox(mls) assert bb[0] == -130.95 assert bb[1] == -34.25 assert bb[2] == 23.15 assert bb[3] == 77.95
def test_bbox_polygon_feature(): p = Polygon([[(2.38, 57.322), (23.194, -20.28), (-120.43, 19.15), (2.38, 57.322)]]) bbox_poly = bbox_polygon(bbox(p)) assert bbox_poly["geometry"]["coordinates"] == [[ [-120.43, -20.28], [23.194, -20.28], [23.194, 57.322], [-120.43, 57.322], [-120.43, -20.28], ]]
def test_bbox_multi_polygon(): mp = MultiPolygon( [ ([(3.78, 9.28), (-130.91, 1.52), (35.12, 72.234), (3.78, 9.28)],), ([(23.18, -34.29), (-1.31, -4.61), (3.41, 77.91), (23.18, -34.29),],), ] ) bb = bbox(mp) assert bb[0] == -130.91 assert bb[1] == -34.29 assert bb[2] == 35.12 assert bb[3] == 77.91
def divide_bbox( feature: dict, cell_width: Optional[float] = None, units: Optional[str] = "m", ): """ Divides the given feature into grid boxes as per given cell width :param feature: Feature to be divide in grid :param cell_width: Width of each grid boxs :param units: Units for the width of grid boxs :return: List of features in which the input feature is divided """ bb = bbox(feature) bbox_polygon_feature = bbox_polygon(bb) if not cell_width: gr = grid( bb, length(bbox_polygon_feature, units=units) / 4, length(bbox_polygon_feature, units=units) / 4, units, ) else: gr = grid(bb, cell_width, cell_width, units) final = [] for f in gr["features"]: try: inter = intersect([f, feature]) if inter: final.append(inter) except Exception: logger.debug("The intersection geometry is incorrect") return final
def getOsm(myGeojson): myUuid = uuid.uuid1() myBbox = bbox(myGeojson) myFeature = myGeojson['features'][0] myProperties = myFeature['properties'] country = myProperties['country'].lower().replace(" ", "_") myId = myProperties['id'] os.makedirs("/data/osm/" + country, exist_ok=True) osmFile = "/data/osm/" + country + "/" + myId + ".osm" osmFileTmp = "/tmp/" + country + "_" + myId + "_" + str(myUuid) + ".osm" osmFileFiltered = "/tmp/" + country + "_" + myId + "_" + str( myUuid) + "_filtered.osm" bounds_file = "/tmp/" + country + "_" + myId + "_" + str( myUuid) + "_bounds.geojson" osmCksumFile = "/data/osm/" + country + "/" + myId + ".cksum" updateRequested = False if (myProperties.update == "1m"): print('Update every:' + myProperties.update) updateRequested = True elif (not os.path.isfile(osmFile)): print('file does not exist yet:' + osmFile) updateRequested = True else: a_file = open(osmFile) line1 = a_file.readline() line2 = a_file.readline() if (not '<osm version=' in line2): print('file content is not OK:' + osmFile) updateRequested = True if (updateRequested): overpass = "******" + \ str(myBbox[0]) + "," + str(myBbox[1]) + "," + \ str(myBbox[2]) + "," + str(myBbox[3]) print("request: " + overpass) try: overpass_request = requests.get(overpass) except: sys.exit("Cannot get osm") with open(osmFileTmp, 'wb') as osm_file_tmp: osm_file_tmp.write(overpass_request.content) a_file = open(osmFileTmp) line1 = a_file.readline() line2 = a_file.readline() if ('<osm version=' in line2): shutil.move(osmFileTmp, osmFile) # os.rename(osmFileTmp, osmFile) # Filter with bound with open(bounds_file, 'w') as outfile: json.dump(myGeojson, outfile, ensure_ascii=False) filter(osmFile, bounds_file, osmFileFiltered) shutil.move(osmFileFiltered, osmFile) # os.rename(osmFileFiltered, osmFile) # cksum osmContent = Path(osmFile).read_text() cksum = hashlib.md5(osmContent.encode('utf-8')).hexdigest() open(osmCksumFile, "w+").write(cksum) print("downloaded: " + osmFile) print("cksum: " + str(cksum)) else: print("download failed") else: print(osmFile + " already downloaded")
def getStations(): """ Takes the query inputs, returns filtered list of station that fit the query criteria. Parameters: lat - latitude lng - longitude start - start date end - end date net - net (in degrees) Returns: results - list of stations count - number of stations doc_id - id of the document in database that contains retrieved data extent - extent under consideration """ # store the request parameters in variables lat = request.get_json().get('lat') print(lat) lng = request.get_json().get('lng') print(lng) start = request.get_json().get('start') print(start) end = request.get_json().get('end') print(end) net = request.get_json().get('net') print(net) # use latitude, longitude and net (in degrees) to generate values for extent center = Feature(geometry=Point((float(lng), float(lat)))) cc = circle(center, radius=int(net), steps=10, units='deg') print(json.dumps(cc, indent=4, sort_keys=True)) print(bbox(cc)) bbox_list = list(bbox(cc)) extent = str(bbox_list[1]) + "," + str(bbox_list[0]) + "," + str( bbox_list[3]) + "," + str(bbox_list[2]) print(extent) # Initialize Firestore DB if not already initialized cred = credentials.Certificate('key.json') if not firebase_admin._apps: firebase_admin.initialize_app(cred) db = firestore.client() # construct query to check if the data exists in the database query_ref = db.collection(u'queries').where( u'latitude', u'==', u'{}'.format(lat)).where(u'longitude', u'==', u'{}'.format(lng)).where( u'net', u'==', u'{}'.format(net)).where( u'extent', u'==', u'{}'.format(extent)).where( u'startDate', u'==', u'{}'.format(start)).where(u'endDate', u'==', u'{}'.format(end)) docs = query_ref.stream() # check length of docs listSize = len(list(docs)) print(listSize) # if data doesn't exist in database, fetch data from API, store in database and return the query results if listSize == 0: # set the user parameters to NCDC URL and get filtered results url = "https://www.ncdc.noaa.gov/cdo-web/api/v2/stations?limit=1000&datasetid=NORMAL_DLY&datatypeid=DLY-TAVG-NORMAL&datatypeid=DLY-TAVG-STDDEV&extent={}&startdate={}&enddate={}".format( extent, start, end) token = "ylPeWbpuHSsbXHmtqurCJXfejdryavRe" headers = { 'token': token, 'Content-Type': 'application/json; charset=utf-8' } response = requests.get(url, headers=headers).text response_info = json.loads(response) if len(response_info) == 0: return jsonify({ "Info": "No results available to match the query. Please submit modified query." }), 200 # create a new doc entry for the user query in firebase doc_id = str(uuid.uuid4().hex) # store retrieved data in firestore doc_ref = db.collection(u'queries').document(u'{}'.format(doc_id)) doc_ref.set({ u'id': u'{}'.format(doc_id), u'latitude': u'{}'.format(lat), u'longitude': u'{}'.format(lng), u'net': u'{}'.format(net), u'startDate': u'{}'.format(start), u'endDate': u'{}'.format(end), u'extent': u'{}'.format(extent), u'dataCoverage': u'', u'results': response_info['results'], u'resultsCount': response_info['metadata']['resultset']['count'] }) return jsonify({ "results": response_info['results'], "count": response_info['metadata']['resultset']['count'], "doc_id": doc_id, "extent": extent }), 200 # if data exists, return the query results from database else: docId = '' # get id of the query document which contains same query docs = query_ref.stream() for doc in docs: docId = doc.id # fetch the document by obtained document id doc_rf = db.collection(u'queries').document(u'{}'.format(docId)) doc = doc_rf.get() if doc.exists: response_info = doc.to_dict() return jsonify({ "results": response_info['results'], "count": response_info['resultsCount'], "doc_id": docId, "extent": response_info['extent'] }), 200 else: return jsonify({ "results": [], "count": 0, "doc_id": "", "extent": "" }), 200