def test_polygon(self): p = geometry.from_wkt( "POLYGON((-91.611 76.227,-91.543 76.217," "-91.503 76.222,-91.483 76.221,-91.474 76.211," "-91.484 76.197,-91.512 76.193,-91.624 76.2," "-91.638 76.202,-91.647 76.211,-91.648 76.218," "-91.643 76.221,-91.636 76.222,-91.611 76.227))" ) self.assertEqual(p.exterior.coords[0][0], -91.611) self.assertEqual(p.exterior.coords[0], p.exterior.coords[-1]) self.assertEqual(len(p.exterior.coords), 14) p = geometry.from_wkt("POLYGON((1 1,5 1,5 5,1 5,1 1),(2 2, 3 2, " "3 3, 2 3,2 2))") self.assertEqual(p.exterior.coords[0], p.exterior.coords[-1]) self.assertEqual(p.exterior.coords[0], (1.0, 1.0)) self.assertEqual(len(list(p.interiors)), 1) self.assertEqual(list(p.interiors)[0].coords, ((2.0, 2.0), (3.0, 2.0), (3.0, 3.0), (2.0, 3.0), (2.0, 2.0))) self.assertEqual( p.to_wkt(), "POLYGON((1.0 1.0, 5.0 1.0, 5.0 5.0, " "1.0 5.0, 1.0 1.0),(2.0 2.0, 3.0 2.0, " "3.0 3.0, 2.0 3.0, 2.0 2.0))", ) p = geometry.from_wkt("POLYGON ((30 10, 10 20, 20 40, 40 40, 30 10))") self.assertEqual(p.exterior.coords[0], p.exterior.coords[-1]) p = geometry.from_wkt( """POLYGON ((35 10, 10 20, 15 40, 45 45, 35 10), (20 30, 35 35, 30 20, 20 30))""" ) self.assertEqual(p.exterior.coords[0], p.exterior.coords[-1])
def test_multipolygon(self): p = geometry.from_wkt( "MULTIPOLYGON(((0 0,10 20,30 40,0 0)," "(1 1,2 2,3 3,1 1))," "((100 100,110 110,120 120,100 100)))" ) # two polygons: the first one has an interior ring self.assertEqual(len(p.geoms), 2) self.assertEqual(p.geoms[0].exterior.coords, ((0.0, 0.0), (10.0, 20.0), (30.0, 40.0), (0.0, 0.0))) self.assertEqual(list(p.geoms[0].interiors)[0].coords, ((1.0, 1.0), (2.0, 2.0), (3.0, 3.0), (1.0, 1.0))) self.assertEqual(p.geoms[1].exterior.coords, ((100.0, 100.0), (110.0, 110.0), (120.0, 120.0), (100.0, 100.0))) self.assertEqual( p.to_wkt(), "MULTIPOLYGON(((0.0 0.0, 10.0 20.0, " "30.0 40.0, 0.0 0.0)," "(1.0 1.0, 2.0 2.0, 3.0 3.0, 1.0 1.0))" "((100.0 100.0, 110.0 110.0," " 120.0 120.0, 100.0 100.0)))", ) p = geometry.from_wkt("MULTIPOLYGON(((1 1,5 1,5 5,1 5,1 1)," "(2 2, 3 2, 3 3, 2 3,2 2)),((3 3,6 2,6 4,3 3)))") self.assertEqual(len(p.geoms), 2) p = geometry.from_wkt("MULTIPOLYGON (((30 20, 10 40, 45 40, 30 20))," "((15 5, 40 10, 10 20, 5 10, 15 5)))") self.assertEqual( p.__geo_interface__, { "type": "MultiPolygon", "bbox": (5.0, 5.0, 45.0, 40.0), "coordinates": ( (((30.0, 20.0), (10.0, 40.0), (45.0, 40.0), (30.0, 20.0)),), (((15.0, 5.0), (40.0, 10.0), (10.0, 20.0), (5.0, 10.0), (15.0, 5.0)),), ), }, )
def test_multilinestring(self): p = geometry.from_wkt('MULTILINESTRING((3 4,10 50,20 25),(-5 -8,-10 -8,-15 -4))') self.assertEqual(p.geoms[0].coords, (((3, 4),(10, 50),(20, 25)))) self.assertEqual(p.geoms[1].coords, (((-5, -8),(-10, -8),(-15, -4)))) self.assertEqual(p.to_wkt(),'MULTILINESTRING((3.0 4.0, 10.0 50.0, 20.0 25.0),(-5.0 -8.0, -10.0 -8.0, -15.0 -4.0))') p = geometry.from_wkt('''MULTILINESTRING ((10 10, 20 20, 10 40), (40 40, 30 30, 40 20, 30 10))''')
def test_multipoint(self): p = geometry.from_wkt("MULTIPOINT(3.5 5.6,4.8 10.5)") self.assertEqual(isinstance(p, geometry.MultiPoint), True) self.assertEqual(p.geoms[0].x, 3.5) self.assertEqual(p.geoms[1].y, 10.5) self.assertEqual(p.to_wkt(), "MULTIPOINT(3.5 5.6, 4.8 10.5)") p = geometry.from_wkt("MULTIPOINT ((10 40), (40 30), " "(20 20), (30 10))") self.assertEqual(isinstance(p, geometry.MultiPoint), True) self.assertEqual(p.geoms[0].x, 10.0) self.assertEqual(p.geoms[3].y, 10.0) p = geometry.from_wkt("MULTIPOINT (10 40, 40 30, 20 20, 30 10)") self.assertEqual(isinstance(p, geometry.MultiPoint), True) self.assertEqual(p.geoms[0].x, 10.0) self.assertEqual(p.geoms[3].y, 10.0)
def test_multipolygon(self): p = geometry.from_wkt('MULTIPOLYGON(((0 0,10 20,30 40,0 0),(1 1,2 2,3 3,1 1)),((100 100,110 110,120 120,100 100)))') #two polygons: the first one has an interior ring self.assertEqual(len(p.geoms), 2) self.assertEqual(p.geoms[0].exterior.coords, ((0.0, 0.0), (10.0, 20.0) , (30.0, 40.0), (0.0, 0.0))) self.assertEqual(list(p.geoms[0].interiors)[0].coords, ((1.0, 1.0), (2.0, 2.0), (3.0, 3.0), (1.0, 1.0))) self.assertEqual(p.geoms[1].exterior.coords, ((100.0, 100.0), (110.0, 110.0), (120.0, 120.0), (100.0, 100.0))) self.assertEqual(p.to_wkt(), 'MULTIPOLYGON(((0.0 0.0, 10.0 20.0, 30.0 40.0, 0.0 0.0),(1.0 1.0, 2.0 2.0, 3.0 3.0, 1.0 1.0))((100.0 100.0, 110.0 110.0, 120.0 120.0, 100.0 100.0)))') p = geometry.from_wkt('MULTIPOLYGON(((1 1,5 1,5 5,1 5,1 1),(2 2, 3 2, 3 3, 2 3,2 2)),((3 3,6 2,6 4,3 3)))') self.assertEqual(len(p.geoms), 2)
def test_linestring(self): l = geometry.from_wkt('LINESTRING(-72.991 46.177,-73.079 46.16,' '-73.146 46.124,-73.177 46.071,-73.164 46.044)') self.assertEqual(l.to_wkt(), 'LINESTRING (-72.991 46.177, ' '-73.079 46.16, -73.146 46.124, ' '-73.177 46.071, -73.164 46.044)') self.assertEqual(isinstance(l, geometry.LineString), True)
def get_bounding_box(wkt): geom = geometry.from_wkt(wkt) minx = geom.bounds[0] miny = geom.bounds[1] maxx = geom.bounds[2] maxy = geom.bounds[3] return str(minx) + ',' + str(miny) + ',' + str(maxx) + ',' + str(maxy)
def create_leaflet_viewer(self, idaho_image_results, output_filename): '''Create a leaflet viewer html file for viewing idaho images Args: idaho_image_results (dict): IDAHO image result set as returned from the catalog. output_filename (str): where to save an output html file ''' description = self.describe_images(idaho_image_results) if len(description) > 0: functionstring = '' for catid, images in description.items(): for partnum, part in images['parts'].items(): num_images = len(list(part.keys())) partname = None if num_images == 1: # there is only one image, use the PAN partname = [p for p in list(part.keys())][0] pan_image_id = '' elif num_images == 2: # there are two images in this part, use the multi (or pansharpen) partname = [p for p in list(part.keys()) if p is not 'PAN'][0] pan_image_id = part['PAN']['id'] if not partname: self.logger.debug("Cannot find part for idaho image.") continue bandstr = { 'RGBN': '0,1,2', 'WORLDVIEW_8_BAND': '4,2,1', 'PAN': '0' }.get(partname, '0,1,2') part_boundstr_wkt = part[partname]['boundstr'] part_polygon = geometry.from_wkt(part_boundstr_wkt) bucketname = part[partname]['bucket'] image_id = part[partname]['id'] W, S, E, N = part_polygon.bounds functionstring += "addLayerToMap('%s','%s',%s,%s,%s,%s,'%s');\n" % (bucketname, image_id, W,S,E,N, pan_image_id) __location__ = os.path.realpath( os.path.join(os.getcwd(), os.path.dirname(__file__))) with open(os.path.join(__location__, 'leafletmap_template.html'), 'r') as htmlfile: data=htmlfile.read().decode("utf8") data = data.replace('FUNCTIONSTRING',functionstring) data = data.replace('CENTERLAT',str(S)) data = data.replace('CENTERLON',str(W)) data = data.replace('BANDS',bandstr) data = data.replace('TOKEN',self.gbdx_connection.access_token) with codecs.open(output_filename,'w','utf8') as outputfile: self.logger.debug("Saving %s" % output_filename) outputfile.write(data) else: print("No items returned.")
def test_geometrycollection(self): gc = geometry.from_wkt('GEOMETRYCOLLECTION(POINT(4 6), LINESTRING(4 6,7 10))') self.assertEqual(len(list(gc.geoms)), 2) self.assertTrue(isinstance(list(gc.geoms)[0], geometry.Point)) self.assertTrue(isinstance(list(gc.geoms)[1], geometry.LineString)) self.assertEqual(gc.to_wkt(), 'GEOMETRYCOLLECTION (POINT (4.0 6.0), LINESTRING (4.0 6.0, 7.0 10.0))')
def verify_wkt(data): try: from shapely import wkt geom = wkt.loads(data) except ImportError: from pygeoif.geometry import from_wkt geom = from_wkt(data) return geom
def wkt(self, data): if data is None or len(data) == 0: return False try: wkt = geometry.from_wkt(data) return isinstance(wkt, geometry._Geometry) except: return False
def verifyWkt(self, data): try: from shapely import wkt geom = wkt.loads(data) #geom = {"type":"Point","coordinates":"00000000"} except ImportError: from pygeoif.geometry import from_wkt geom = from_wkt(data) return geom
def test_point(self): p = geometry.from_wkt("POINT (0.0 1.0)") self.assertEqual(isinstance(p, geometry.Point), True) self.assertEqual(p.x, 0.0) self.assertEqual(p.y, 1.0) self.assertEqual(p.to_wkt(), "POINT (0.0 1.0)") self.assertEqual(p.to_wkt(), p.wkt) self.assertEqual(str(p), "POINT (0.0 1.0)") self.assertEqual(p.geom_type, "Point")
def query_iteratively(self, searchAreaWkt, query, count=100, ttl='5m'): ''' Perform a vector services query using the QUERY API (https://gbdxdocs.digitalglobe.com/docs/vs-query-list-vector-items-returns-default-fields) Args: searchAreaWkt: WKT Polygon of area to search query: Elastic Search query count: Maximum number of results to return ttl: Amount of time for each temporary vector page to exist Returns: generator of vector results ''' search_area_polygon = geometry.from_wkt(searchAreaWkt) left, lower, right, upper = search_area_polygon.bounds params = { "q": query, "count": count, "ttl": ttl, "left": left, "right": right, "lower": lower, "upper": upper } # initialize paging request r = self.gbdx_connection.get(self.query_url, params=params) r.raise_for_status() page = r.json() paging_id = page['pagingId'] item_count = int(page['itemCount']) # get vectors from each page while paging_id and item_count > 0: headers = {'Content-Type':'application/x-www-form-urlencoded'} data = { "pagingId": paging_id, "ttl": ttl } r = self.gbdx_connection.post(self.page_url, headers=headers, data=data) r.raise_for_status() page = r.json() paging_id = page['next_paging_id'] item_count = int(page['item_count']) data = page['data'] for vector in data: yield vector
def coordinates(self, value): if value: try: from shapely import wkt geom = wkt.loads(value) except ImportError: from pygeoif.geometry import from_wkt geom = from_wkt(value) coords = geom.__geo_interface__ geo = IWriteGeoreferenced(self.context) geo.setGeoInterface(coords['type'], coords['coordinates'])
def records_in_polygon(records,polygon): # Filter out the records that are not inside the polygon output_records = [] for record in records: recordwkt = record['properties']['footprintWkt'] record_polygon = geometry.from_wkt(recordwkt) if bbox_in_poly(record_polygon,polygon): output_records.append(record) #print "Filtered in polygon: %s" % len(output_records) return output_records
def search_materials_in_multiple_small_searches(search_request, gbdx_connection, base_url): D = 1.4 # the size in degrees of the side of a square that we will search searchAreaWkt = search_request['searchAreaWkt'] searchAreaPolygon = geometry.from_wkt(searchAreaWkt) W, S, E, N = searchAreaPolygon.bounds Ys = [i for i in xfrange(S,N,D)] Xs = [i for i in xfrange(W,E,D)] # Handle point searches: if W == E and N == S: Ys = [S, N] Xs = [W, E] # print Xs # print Ys # print searchAreaWkt records = [] # Loop pairwise row = 0 col = 0 for y, y1 in zip(Ys, Ys[1:]): row = row + 1 for x, x1 in zip(Xs, Xs[1:]): col = col + 1 bbox = (x, y, x1, y1) subsearchpoly = polygon_from_bounds(bbox) # # verify that the subsearchpoly is inside the searchAreaPolygon. If not break. if not bbox_in_poly(subsearchpoly,searchAreaPolygon) and not bbox_in_poly(searchAreaPolygon, subsearchpoly) and not (y == y1 and x == x1): pass else: search_request['searchAreaWkt'] = subsearchpoly.wkt url = '%(base_url)s/search?includeRelationships=false' % { 'base_url': base_url } headers = {'Content-Type':'application/json'} r = gbdx_connection.post(url, headers=headers, data=json.dumps(search_request)) r.raise_for_status() records = records + r.json()['results'] records = dedup_records(records) # this next line works, but filters too much stuff. It removes some items intersecting the polygon. #records = records_in_polygon(records, searchAreaPolygon) # this takes quite a while to run, so leave it commented return records
def exportKML(self): k = kml.KML() ns = '{http://www.opengis.net/kml/2.2}' d = kml.Document(ns, '0', 'Export', 'Exported from Lowiki') k.append(d) f = kml.Folder(ns, '1', 'Locations', 'Locations in Lowiki') d.append(f) for page in self._get_export_pages(): try: p = kml.Placemark(ns, str(page.id), page.name, '') p.geometry = geometry.from_wkt(page.mapdata.geom.wkt) f.append(p) except: pass filename = '/tmp/export.kml' with open(filename, 'w') as f: f.write(k.to_string().encode('utf-8')) f.close() self.file = open(filename, 'r') self.file_mime_type = 'application/vnd.google-earth.kml+xml'
def get_tms_layers(self, catid, bands='4,2,1', gamma=1.3, highcutoff=0.98, lowcutoff=0.02, brightness=1.0, contrast=1.0): '''Get list of urls and bounding boxes corrsponding to idaho images for a given catalog id. Args: catid (str): Catalog id bands (str): Bands to display, separated by commas (0-7). gamma (float): gamma coefficient. This is for on-the-fly pansharpening. highcutoff (float): High cut off coefficient (0.0 to 1.0). This is for on-the-fly pansharpening. lowcutoff (float): Low cut off coefficient (0.0 to 1.0). This is for on-the-fly pansharpening. brightness (float): Brightness coefficient (0.0 to 1.0). This is for on-the-fly pansharpening. contrast (float): Contrast coefficient (0.0 to 1.0). This is for on-the-fly pansharpening. Returns: urls (list): TMS urls. bboxes (list of tuples): Each tuple is (W, S, E, N) where (W,S,E,N) are the bounds of the corresponding idaho part. ''' description = self.describe_images(self.get_images_by_catid(catid)) service_url = 'http://idaho.geobigdata.io/v1/tile/' urls, bboxes = [], [] for catid, images in description.items(): for partnum, part in images['parts'].items(): if 'PAN' in part.keys(): pan_id = part['PAN']['id'] if 'WORLDVIEW_8_BAND' in part.keys(): ms_id = part['WORLDVIEW_8_BAND']['id'] ms_partname = 'WORLDVIEW_8_BAND' elif 'RGBN' in part.keys(): ms_id = part['RGBN']['id'] ms_partname = 'RGBN' if ms_id: if pan_id: band_str = ms_id + '/{z}/{x}/{y}?bands=' + bands + '&panId=' + pan_id else: band_str = ms_id + '/{z}/{x}/{y}?bands=' + bands bbox = geometry.from_wkt(part[ms_partname]['boundstr']).bounds elif not ms_id and pan_id: band_str = pan_id + '/{z}/{x}/{y}?bands=0' bbox = geometry.from_wkt(part['PAN']['boundstr']).bounds else: continue bboxes.append(bbox) # Get the bucket. It has to be the same for all entries in the part. bucket = part[part.keys()[0]]['bucket'] # Get the token token = self.gbdx_connection.access_token # Assemble url url = (service_url + bucket + '/' + band_str + """&gamma={} &highCutoff={} &lowCutoff={} &brightness={} &contrast={} &token={}""".format(gamma, highcutoff, lowcutoff, brightness, contrast, token)) urls.append(url) return urls, bboxes
def test_wkt_ok(self): for wkt in self.wkt_ok: geometry.from_wkt(wkt)
def query_iteratively(self, searchAreaWkt, query, count=100, ttl='5m', index=default_index): ''' Perform a vector services query using the QUERY API (https://gbdxdocs.digitalglobe.com/docs/vs-query-list-vector-items-returns-default-fields) Args: searchAreaWkt: WKT Polygon of area to search query: Elastic Search query count: Maximum number of results to return ttl: Amount of time for each temporary vector page to exist Returns: generator of vector results ''' search_area_polygon = geometry.from_wkt(searchAreaWkt) left, lower, right, upper = search_area_polygon.bounds params = { "q": query, "count": count, "ttl": ttl, "left": left, "right": right, "lower": lower, "upper": upper } # initialize paging request url = self.query_index_url % index if index else self.query_url r = self.gbdx_connection.get(url, params=params) r.raise_for_status() page = r.json() paging_id = page['next_paging_id'] item_count = int(page['item_count']) data = page['data'] for vector in data: yield vector # get vectors from each page while paging_id and item_count > 0: headers = {'Content-Type': 'application/x-www-form-urlencoded'} data = {"pagingId": paging_id, "ttl": ttl} r = self.gbdx_connection.post(self.page_url, headers=headers, data=data) r.raise_for_status() page = r.json() paging_id = page['next_paging_id'] item_count = int(page['item_count']) data = page['data'] for vector in data: yield vector
def view_idaho_tiles_by_bbox(self, catId, bbox, output_filename): '''Retrieve and view just the IDAHO chips in a particular bounding box for a catID. Args: catid (str): The source catalog ID from the platform catalog. bbox (list): List of coords: minx(W), miny(S), maxx(E), maxy(N). output_filename (str): a Leaflet Viewer file showing the IDAHO images as tiles. ''' minx, miny, maxx, maxy = bbox #validate bbox values if (minx > maxx): print('The west value is not less than the east value.') exit if (miny > maxy): print('The south value is not less than the north value.') exit #create bbox polygon bp1 = Point(minx, miny) bp2 = Point(minx, maxy) bp3 = Point(maxx, maxy) bp4 = Point(maxx, miny) bbox_polygon = Polygon(bp1, bp2, bp3, bp4) #get IDAHO image results: parts idaho_image_results = self.get_images_by_catid(catId) description = self.describe_images(idaho_image_results) tile_count = 0 for catid, images in description.items(): functionstring = '' for partnum, part in images['parts'].items(): num_images = len(list(part.keys())) partname = None if num_images == 1: # there is only one image, use the PAN partname = [ p for p in list(part.keys()) if p.upper() == 'PAN' ][0] pan_image_id = '' elif num_images == 2: # there are two images in this part, use the multi (or pansharpen) partname = [ p for p in list(part.keys()) if p is not 'PAN' ][0] pan_image_id = part['PAN']['id'] if not partname: print("Cannot find part for idaho image.") continue bandstr = { 'RGBN': '0,1,2', 'WORLDVIEW_8_BAND': '4,2,1', 'PAN': '0' }.get(partname, '0,1,2') part_boundstr_wkt = part[partname]['boundstr'] part_polygon = geometry.from_wkt(part_boundstr_wkt) bucketname = part[partname]['bucket'] image_id = part[partname]['id'] W, S, E, N = part_polygon.bounds pp1, pp2, pp3, pp4 = Point(W, S), Point(W, N), Point(E, N), Point(E, S) part_bbox_polygon = Polygon(pp1, pp2, pp3, pp4) if (bbox_polygon.intersection(part_bbox_polygon)): functionstring += ( "addLayerToMap('%s','%s',%s,%s,%s,%s,'%s');\n" % (bucketname, image_id, W, S, E, N, pan_image_id)) tile_count += 1 print('There were ' + str(tile_count) + ' IDAHO images found to ' + 'intersect with the provided bounding box.') __location__ = os.path.realpath( os.path.join(os.getcwd(), os.path.dirname(os.path.realpath('__file__')))) with open(os.path.join(__location__, 'leafletmap_template.html'), 'r') as htmlfile: data = htmlfile.read().decode("utf8") data = data.replace('FUNCTIONSTRING', functionstring) data = data.replace('CENTERLAT', str(S + old_div((N - S), 2))) data = data.replace('CENTERLON', str(W + old_div((E - W), 2))) data = data.replace('BANDS', bandstr) data = data.replace('TOKEN', self.gbdx_connection.access_token) with codecs.open(output_filename, 'w', 'utf8') as outputfile: print("Saving %s" % output_filename) outputfile.write(data)
def test_linearring(self): r = geometry.from_wkt("LINEARRING (0 0,0 1,1 0,0 0)") self.assertEqual(isinstance(r, geometry.LinearRing), True) self.assertEqual(r.to_wkt(), "LINEARRING (0.0 0.0, 0.0 1.0, " "1.0 0.0, 0.0 0.0)")
def project_wkt(resource_uri, graph_uri, ntriples, config, post_handler=postfragment, logger=None): graph = rdflib.Graph() try: graph.parse(data=ntriples, format="nt") except Exception as e: s = "Failed to parse input fragment '%s'... RDFLib says the reason is '%s'" % (ntriples, e.message) if logger: logger.exception(s) raise e subject_filter = rdflib.term.URIRef(resource_uri) wkt_uri = rdflib.term.URIRef(config['sourceGeometry']) wkt_res = [t for t in graph.triples((subject_filter, wkt_uri, None))] if graph.__len__() == 0: if logger: s = "Received empty fragment. Deleting'%s'" % config['sourceGeometry'] logger.info(s) post_handler(config["endpoint"], resource_uri, graph_uri, ntriples, logger=logger) return True if len(wkt_res) == 0: if logger: s = "No geometry matching predicate '%s' found in fragment, skipping..." % config['sourceGeometry'] logger.warning(s) return True triples = "" source_projection = Proj(init=config['sourceCoordinateSystem']) target_projection = Proj(init=config['targetCoordinateSystem']) for s, p, value in wkt_res: p = geometry.from_wkt(value.strip()) if isinstance(p, geometry.Polygon): projected_s = "POLYGON " + project_wkt_polygon(config, p, source_projection, target_projection, logger=logger) + "" triples += '<%s> <%s> "%s"^^<%s> .\n' % (resource_uri, config['targetGeometry'], projected_s, config.get("targetDatatype", "http://www.w3.org/2001/XMLSchema#string")) elif isinstance(p, geometry.MultiPolygon): polygons = [] for polygon in p.geoms: polygons.append(project_wkt_polygon(config, polygon, source_projection, target_projection, logger=logger)) projected_s = "MULTIPOLYGON (" + ", ".join(polygons) + ")" triples += '<%s> <%s> "%s"^^<%s> .\n' % (resource_uri, config['targetGeometry'], projected_s, config.get("targetDatatype", "http://www.w3.org/2001/XMLSchema#string")) elif isinstance(p, geometry.Point): projected_s = "POINT (" + project_wkt_point(config, p, source_projection, target_projection, logger=logger) + ")" triples += '<%s> <%s> "%s"^^<%s> .\n' % (resource_uri, config['targetGeometry'], projected_s, config.get("targetDatatype", "http://www.w3.org/2001/XMLSchema#string")) post_handler(config["endpoint"], resource_uri, graph_uri, triples, logger=logger) return True
def download_idaho_tiles_by_bbox(self, catId, bbox, resolution, outputfolder): '''Retrieve and view just the IDAHO chips in a particular bounding box for a catID. Args: catid (str): The source catalog ID from the platform catalog. bbox (list): List of coords: minx(W), miny(S), maxx(E), maxy(N). resolution (str): The desired floating point resolution of the tiles. outputfolder (str): The desired output location of the IDAHO tiles. ''' minx, miny, maxx, maxy = bbox #validate bbox values if (minx > maxx): print('The west value is not less than the east value.') exit if (miny > maxy): print('The south value is not less than the north value.') exit #create bbox polygon bp1 = Point(minx, miny) bp2 = Point(minx, maxy) bp3 = Point(maxx, maxy) bp4 = Point(maxx, miny) bbox_polygon = Polygon(bp1, bp2, bp3, bp4) #get IDAHO image results: parts idaho_image_results = self.get_images_by_catid(catId) description = self.describe_images(idaho_image_results) tile_count = 0 for catid, images in description.items(): for partnum, part in images['parts'].items(): num_images = len(list(part.keys())) partname = None if num_images == 1: # there is only one image, use the PAN partname = [ p for p in list(part.keys()) if p.upper() == 'PAN' ][0] elif num_images == 2: # there are two images in this part, use the multi (or pansharpen) partname = [ p for p in list(part.keys()) if p is not 'PAN' ][0] if not partname: print("Cannot find part for idaho image.") continue part_boundstr_wkt = part[partname]['boundstr'] part_polygon = geometry.from_wkt(part_boundstr_wkt) bucketname = part[partname]['bucket'] image_id = part[partname]['id'] W, S, E, N = part_polygon.bounds pp1, pp2, pp3, pp4 = Point(W, S), Point(W, N), Point(E, N), Point(E, S) part_bbox_polygon = Polygon(pp1, pp2, pp3, pp4) if (bbox_polygon.intersection(part_bbox_polygon)): center_lat = (S + old_div((N - S), 2)) center_lon = (W + old_div((E - W), 2)) print(center_lat, center_lon) self.get_idaho_chip(bucket_name=bucketname, idaho_id=image_id, center_lat=str(center_lat), center_lon=str(center_lon), resolution=resolution, output_folder=outputfolder) tile_count += 1 print('There were ' + str(tile_count) + ' IDAHO images downloaded that ' + 'intersect with the provided bounding box.')
def get_tms_layers(self, catid, bands='4,2,1', gamma=1.3, highcutoff=0.98, lowcutoff=0.02, brightness=1.0, contrast=1.0): '''Get list of urls and bounding boxes corrsponding to idaho images for a given catalog id. Args: catid (str): Catalog id bands (str): Bands to display, separated by commas (0-7). gamma (float): gamma coefficient. This is for on-the-fly pansharpening. highcutoff (float): High cut off coefficient (0.0 to 1.0). This is for on-the-fly pansharpening. lowcutoff (float): Low cut off coefficient (0.0 to 1.0). This is for on-the-fly pansharpening. brightness (float): Brightness coefficient (0.0 to 1.0). This is for on-the-fly pansharpening. contrast (float): Contrast coefficient (0.0 to 1.0). This is for on-the-fly pansharpening. Returns: urls (list): TMS urls. bboxes (list of tuples): Each tuple is (W, S, E, N) where (W,S,E,N) are the bounds of the corresponding idaho part. ''' description = self.describe_images(self.get_images_by_catid(catid)) service_url = 'http://idaho.geobigdata.io/v1/tile/' urls, bboxes = [], [] for catid, images in description.items(): for partnum, part in images['parts'].items(): if 'PAN' in part.keys(): pan_id = part['PAN']['id'] if 'WORLDVIEW_8_BAND' in part.keys(): ms_id = part['WORLDVIEW_8_BAND']['id'] ms_partname = 'WORLDVIEW_8_BAND' elif 'RGBN' in part.keys(): ms_id = part['RGBN']['id'] ms_partname = 'RGBN' if ms_id: if pan_id: band_str = ms_id + '/{z}/{x}/{y}?bands=' + bands + '&panId=' + pan_id else: band_str = ms_id + '/{z}/{x}/{y}?bands=' + bands bbox = geometry.from_wkt( part[ms_partname]['boundstr']).bounds elif not ms_id and pan_id: band_str = pan_id + '/{z}/{x}/{y}?bands=0' bbox = geometry.from_wkt(part['PAN']['boundstr']).bounds else: continue bboxes.append(bbox) # Get the bucket. It has to be the same for all entries in the part. bucket = part[part.keys()[0]]['bucket'] # Get the token token = self.gbdx_connection.access_token # Assemble url url = (service_url + bucket + '/' + band_str + """&gamma={} &highCutoff={} &lowCutoff={} &brightness={} &contrast={} &token={}""".format( gamma, highcutoff, lowcutoff, brightness, contrast, token)) urls.append(url) return urls, bboxes
def create_leaflet_viewer(self, idaho_image_results, filename): '''Create a leaflet viewer html file for viewing idaho images. Args: idaho_image_results (dict): IDAHO image result set as returned from the catalog. filename (str): Where to save output html file. ''' description = self.describe_images(idaho_image_results) if len(description) > 0: functionstring = '' for catid, images in description.items(): for partnum, part in images['parts'].items(): num_images = len(list(part.keys())) partname = None if num_images == 1: # there is only one image, use the PAN partname = [p for p in list(part.keys())][0] pan_image_id = '' elif num_images == 2: # there are two images in this part, use the multi (or pansharpen) partname = [ p for p in list(part.keys()) if p is not 'PAN' ][0] pan_image_id = part['PAN']['id'] if not partname: self.logger.debug("Cannot find part for idaho image.") continue bandstr = { 'RGBN': '0,1,2', 'WORLDVIEW_8_BAND': '4,2,1', 'PAN': '0' }.get(partname, '0,1,2') part_boundstr_wkt = part[partname]['boundstr'] part_polygon = geometry.from_wkt(part_boundstr_wkt) bucketname = part[partname]['bucket'] image_id = part[partname]['id'] W, S, E, N = part_polygon.bounds functionstring += "addLayerToMap('%s','%s',%s,%s,%s,%s,'%s');\n" % ( bucketname, image_id, W, S, E, N, pan_image_id) __location__ = os.path.realpath( os.path.join(os.getcwd(), os.path.dirname(__file__))) with open(os.path.join(__location__, 'leafletmap_template.html'), 'r') as htmlfile: data = htmlfile.read().decode("utf8") data = data.replace('FUNCTIONSTRING', functionstring) data = data.replace('CENTERLAT', str(S)) data = data.replace('CENTERLON', str(W)) data = data.replace('BANDS', bandstr) data = data.replace('TOKEN', self.gbdx_connection.access_token) with codecs.open(filename, 'w', 'utf8') as outputfile: self.logger.debug("Saving %s" % filename) outputfile.write(data) else: print('No items returned.')
def test_linearring(self): r = geometry.from_wkt('LINEARRING (0 0,0 1,1 0,0 0)') self.assertEqual(isinstance(r, geometry.LinearRing), True) self.assertEqual(r.to_wkt(), 'LINEARRING (0.0 0.0, 0.0 1.0, ' '1.0 0.0, 0.0 0.0)')
def view_idaho_tiles_by_bbox(self, catId, bbox, output_filename): '''Retrieve and view just the IDAHO chips in a particular bounding box for a catID. Args: catid (str): The source catalog ID from the platform catalog. bbox (list): List of coords: minx(W), miny(S), maxx(E), maxy(N). output_filename (str): a Leaflet Viewer file showing the IDAHO images as tiles. ''' minx, miny, maxx, maxy = bbox #validate bbox values if (minx > maxx): print ('The west value is not less than the east value.') exit if (miny > maxy): print ('The south value is not less than the north value.') exit #create bbox polygon bp1 = Point(minx, miny) bp2 = Point(minx, maxy) bp3 = Point(maxx, maxy) bp4 = Point(maxx, miny) bbox_polygon = Polygon(bp1, bp2, bp3, bp4) #get IDAHO image results: parts idaho_image_results = self.get_images_by_catid(catId) description = self.describe_images(idaho_image_results) tile_count = 0 for catid, images in description.items(): functionstring = '' for partnum, part in images['parts'].items(): num_images = len(list(part.keys())) partname = None if num_images == 1: # there is only one image, use the PAN partname = [p for p in list(part.keys()) if p.upper() == 'PAN'][0] pan_image_id = '' elif num_images == 2: # there are two images in this part, use the multi (or pansharpen) partname = [p for p in list(part.keys()) if p is not 'PAN'][0] pan_image_id = part['PAN']['id'] if not partname: print("Cannot find part for idaho image.") continue bandstr = { 'RGBN': '0,1,2', 'WORLDVIEW_8_BAND': '4,2,1', 'PAN': '0' }.get(partname, '0,1,2') part_boundstr_wkt = part[partname]['boundstr'] part_polygon = geometry.from_wkt(part_boundstr_wkt) bucketname = part[partname]['bucket'] image_id = part[partname]['id'] W, S, E, N = part_polygon.bounds pp1, pp2, pp3, pp4 = Point(W, S), Point(W, N), Point(E, N), Point(E, S) part_bbox_polygon = Polygon(pp1, pp2, pp3, pp4) if (bbox_polygon.intersection(part_bbox_polygon)): functionstring += ("addLayerToMap('%s','%s',%s,%s,%s,%s,'%s');\n" % (bucketname, image_id, W,S,E,N, pan_image_id)) tile_count += 1 print ('There were ' + str(tile_count) + ' IDAHO images found to ' + 'intersect with the provided bounding box.') __location__ = os.path.realpath( os.path.join(os.getcwd(), os.path.dirname(os.path.realpath('__file__')))) with open(os.path.join(__location__, 'leafletmap_template.html'), 'r') as htmlfile: data=htmlfile.read().decode("utf8") data = data.replace('FUNCTIONSTRING',functionstring) data = data.replace('CENTERLAT',str(S + old_div((N-S),2))) data = data.replace('CENTERLON',str(W + old_div((E-W),2))) data = data.replace('BANDS',bandstr) data = data.replace('TOKEN',self.gbdx_connection.access_token) with codecs.open(output_filename,'w','utf8') as outputfile: print("Saving %s" % output_filename) outputfile.write(data)
def download_idaho_tiles_by_bbox(self, catId, bbox, resolution, outputfolder): '''Retrieve and view just the IDAHO chips in a particular bounding box for a catID. Args: catid (str): The source catalog ID from the platform catalog. bbox (list): List of coords: minx(W), miny(S), maxx(E), maxy(N). resolution (str): The desired floating point resolution of the tiles. outputfolder (str): The desired output location of the IDAHO tiles. ''' minx, miny, maxx, maxy = bbox #validate bbox values if (minx > maxx): print ('The west value is not less than the east value.') exit if (miny > maxy): print ('The south value is not less than the north value.') exit #create bbox polygon bp1 = Point(minx, miny) bp2 = Point(minx, maxy) bp3 = Point(maxx, maxy) bp4 = Point(maxx, miny) bbox_polygon = Polygon(bp1, bp2, bp3, bp4) #get IDAHO image results: parts idaho_image_results = self.get_images_by_catid(catId) description = self.describe_images(idaho_image_results) tile_count = 0 for catid, images in description.items(): for partnum, part in images['parts'].items(): num_images = len(list(part.keys())) partname = None if num_images == 1: # there is only one image, use the PAN partname = [p for p in list(part.keys()) if p.upper() == 'PAN'][0] elif num_images == 2: # there are two images in this part, use the multi (or pansharpen) partname = [p for p in list(part.keys()) if p is not 'PAN'][0] if not partname: print("Cannot find part for idaho image.") continue part_boundstr_wkt = part[partname]['boundstr'] part_polygon = geometry.from_wkt(part_boundstr_wkt) bucketname = part[partname]['bucket'] image_id = part[partname]['id'] W, S, E, N = part_polygon.bounds pp1, pp2, pp3, pp4 = Point(W, S), Point(W, N), Point(E, N), Point(E, S) part_bbox_polygon = Polygon(pp1, pp2, pp3, pp4) if (bbox_polygon.intersection(part_bbox_polygon)): center_lat = (S + old_div((N-S),2)) center_lon = (W + old_div((E-W),2)) print(center_lat, center_lon) self.get_idaho_chip(bucket_name=bucketname, idaho_id=image_id, center_lat=str(center_lat), center_lon=str(center_lon), resolution=resolution, output_folder=outputfolder) tile_count+=1 print ('There were ' + str(tile_count) + ' IDAHO images downloaded that ' + 'intersect with the provided bounding box.')