def deploy_keras_model(dltile, src_product_id, dest_product_id): import tensorflow as tf import numpy as np catalog_client = dl.Catalog() raster_client = dl.Raster() metadata_client = dl.Metadata() # NOTE substitute your own trained model here. model = tf.keras.applications.resnet50.ResNet50() scene = metadata_client.search(src_product_id, geom=raster_client.dltile(dltile), limit=1)['features'][0]['id'] tile, meta = raster_client.ndarray(scene, bands=['red', 'green', 'blue'], scales=[[0, 255]] * 3, ot='Byte', dltile=dltile) # resnet50 expects the shape of the input array to be 4 dimensional, which allows for batch # predictions. tile_for_prediction = tile[np.newaxis, :] pred = model.predict(tile_for_prediction) # get predicted class with a simple maximum of class probabilities. class_ = np.argmax(pred, 1) # create a new raster of the tile area with one channel of the prediction from the model. image = np.full(tile.shape[:-1], class_, dtype=np.uint16) # upload a tile of this "prediction" to catalog image_id = ':'.join([src_product_id, dltile.replace(':', '_')]) catalog_client.upload_ndarray(image, dest_product_id, image_id, raster_meta=meta)
def fc_from_latlong(lat, long, start="2018-01-01", end="2018-12-31", platform="sentinel-2:L1C"): """ Platforms: landsat = "landsat:LC08:01:RT:TOAR" sentinel2 = "sentinel-2:L1C" """ aoi = aoi_from_latlon(lat, long) metadata_client = dl.Metadata() # Image Search fc = metadata_client.search( products=platform, geom=aoi, start_datetime=start, end_datetime=end, limit=365, ) return fc
def __init__(self, aoi, item_type=ItemType.PSScene4Band, product_id=None, title=None, description=None, overwrite=False, start_datetime=None, end_datetime=None, cloud_fraction=1, limit=None, order_id=None, api_key=None): self._planet = p.ClientV1(api_key=api_key) self._catalog = dl.Catalog() self._metadata = dl.Metadata() self._auth = dl.Auth() self._order_id = order_id self._title=title self._description=description self.stats = None self._running = False self._items = [] self.uploads = [] if self._running: raise Exception('Already processing') else: self._running = True self._start_datetime = start_datetime self._end_datetime = end_datetime self._cloud_fraction = cloud_fraction self._limit =limit self._get_items(aoi, [item_type.name]) self._init_product(product_id, item_type=item_type, overwrite=overwrite) item_ids = [item['id'] for item in self._items] scenes = clip_and_download(aoi, item_ids, item_type.name, item_type.bundle, api_key, order_id=self._order_id) for scene_id, scene in scenes.items(): with open(scene['metadata.json']) as meta_file: metadata = json.load(meta_file)['properties'] with open(scene['3B_AnalyticMS_metadata_clip.xml']) as xml_file: xml_meta = xmltodict.parse(xml_file.read()) for band in xml_meta['ps:EarthObservation']['gml:resultOf']['ps:EarthObservationResult']['ps:bandSpecificMetadata']: metadata[f"band_{band['ps:bandNumber']}_radiometricScaleFactor"] = band['ps:radiometricScaleFactor'] metadata[f"band_{band['ps:bandNumber']}_reflectanceCoefficient"] = band['ps:reflectanceCoefficient'] self._upload_image([str(scene[str(file_key)]) for file_key in item_type.files] , metadata, scene_id)
def __init__(self, path_to_cities_geom, path_to_cities_info): self.path_to_cities_geom = path_to_cities_geom self.path_to_cities_info = path_to_cities_info self.cities_geometry = gpd.read_file(self.path_to_cities_geom) if os.path.exists(os.path.join(self.path_to_cities_info)): with open(os.path.join(self.path_to_cities_info), 'r') as infile: self.cities_info = json.loads(infile.read()) else: self.cities_info = defaultdict(dict) self.raster_client = dl.Raster() self.metadata_client = dl.Metadata() self.pixel_area = 225 self.tree_area = 50 self.tree_profit = 1.5
"""Simple example of how to create a product, then add some bands and imagery. We will use the included file `building_mask.tif` as an example of some imagery you might want to upload with the catalog. """ import descarteslabs as dl import os from random import randint from time import sleep catalog_client = dl.Catalog() metadata_client = dl.Metadata() raster_client = dl.Raster() # First step, create a product, which is a descriptive document you use to # group related images. product_id = catalog_client.add_product( "building_mask:osm:v0", title="OSM Building Mask", description= "Rasterized OSM building footprints from vector data. Quality varies regionally", )["data"]["id"] # Next we need to add bands. The core function of a band is to tell us how data # is encoded in the imagery that you are going to upload. For these building # masks there is only one file per scene, and each scene has one 8 bit band. band_id = catalog_client.add_band( product_id=product_id, # id of the product we just created. name="footprint", # this is a unique name to describe what the band encodes.