Ejemplo n.º 1
0
    def testSimplifyFeatureRatio(self):
        result = self.standard(ratio=0.1)

        b = vw.simplify_feature(self.fixture, ratio=0.90)
        assert len(b['geometry']['coordinates']) > len(result['geometry']['coordinates'])
        for i, j in zip(range(1, 9), range(2, 10)):
            r = vw.simplify_feature(self.fixture, ratio=float(i) / 10)
            s = vw.simplify_feature(self.fixture, ratio=float(j) / 10)
            assert len(r['geometry']['coordinates']) <= len(s['geometry']['coordinates'])
Ejemplo n.º 2
0
    def testSimplifyFeatureRatio(self):
        result = self.standard(ratio=0.1)

        b = vw.simplify_feature(self.fixture, ratio=0.90)
        assert len(b['geometry']['coordinates']) > len(
            result['geometry']['coordinates'])
        for i, j in zip(range(1, 9), range(2, 10)):
            r = vw.simplify_feature(self.fixture, ratio=float(i) / 10)
            s = vw.simplify_feature(self.fixture, ratio=float(j) / 10)
            assert len(r['geometry']['coordinates']) <= len(
                s['geometry']['coordinates'])
Ejemplo n.º 3
0
    def standard(self, **kwargs):
        result = vw.simplify_feature(self.fixture, **kwargs)

        assert 'geometry' in result
        assert 'properties' in result
        assert result['properties'] == self.fixture['properties']
        assert self.fixture['geometry']['type'] == result['geometry']['type']
        assert self.fixture['geometry']['coordinates'][0] == result['geometry']['coordinates'][0]

        assert len(self.fixture['geometry']['coordinates']) > len(result['geometry']['coordinates'])

        return result
Ejemplo n.º 4
0
    def standard(self, **kwargs):
        result = vw.simplify_feature(self.fixture, **kwargs)

        assert 'geometry' in result
        assert 'properties' in result
        assert result['properties'] == self.fixture['properties']
        assert self.fixture['geometry']['type'] == result['geometry']['type']
        assert self.fixture['geometry']['coordinates'][0] == result[
            'geometry']['coordinates'][0]

        assert len(self.fixture['geometry']['coordinates']) > len(
            result['geometry']['coordinates'])

        return result
Ejemplo n.º 5
0
def order(**kwargs):
    for key,value in kwargs.items():
        if key=='name':
            dbundle['name']=value
        if key=='sid':
            if value is not None:
                dbundle['subscription_id']=int(value)
            else:
                dbundle.pop('subscription_id',None)
        if key=='item':
            dbundle['products'][0]['item_type']=value
        if key=='asset':
            dbundle['products'][0]['product_bundle']=value
        if key=='idlist':
            l=[]
            if value.endswith('.csv'):
                with open(value) as f:
                    try:
                        reader = csv.reader(f)
                        for row in reader:
                            item_id=row[0]
                            if str(item_id.isalpha())=='False':
                                l.append(item_id)
                    except Exception as e:
                        print('Issue with reading: '+str(value))
            elif value.endswith('.txt'):
                with open(value) as f:
                    for line in f:
                        item_id=line.strip()
                        l.append(item_id)
            dbundle['products'][0]['item_ids'] = l
    k=dbundle
    for key,value in kwargs.items():
        if key=='op' and value!=None:
            for items in value:
                if items=='clip':
                    dbundle['tools'].append(dclip)
                elif items=='toar':
                    dbundle['tools'].append(dtoar)
                elif items=='zip':
                    dbundle.update(dzip)
                elif items=='zipall':
                    dbundle.update(dszip)
                elif items=='email':
                    dbundle.update(demail)
                elif items=='aws':
                    dbundle.update(daws)
                elif items=='azure':
                    dbundle.update(dazure)
                elif items=='gcs':
                    dbundle.update(dgcs)
                elif items=='composite':
                    dbundle['tools'].append(dcomposite)
                elif items=='projection':
                    dbundle['tools'].append(dreproject)
                elif items=='ndvi':
                    dndvi={"pixel_type": "32R","ndvi": "(b4 - b3) / (b4+b3)"}
                    dbmath['bandmath'].update(dndvi)
                elif items=='gndvi':
                    dgndvi={"pixel_type": "32R","gndvi": "(b4 - b2) / (b4+b2)"}
                    dbmath['bandmath'].update(dgndvi)
                elif items=='ndwi':
                    dndwi={"pixel_type": "32R","ndwi": "(b2 - b4) / (b4+b2)"}
                    dbmath['bandmath'].update(dndwi)
                elif items=='bndvi':
                    bndvi={"pixel_type": "32R","bndvi": "(b4-b1)/(b4+b1)"}
                    dbmath['bandmath'].update(bndvi)
                elif items=='tvi':
                    dtvi={"pixel_type": "32R","tvi": "((b4-b3)/(b4+b3)+0.5) ** 0.5"}
                    dbmath['bandmath'].update(dtvi)
                elif items=='osavi':
                    dosavi={"pixel_type": "32R","osavi": "1.16 * (b4-b3)/(b4+b3+0.16)"}
                    dbmath['bandmath'].update(dosavi)
                elif items=='evi2':
                    devi2={"pixel_type": "32R","evi2": "2.5 * (b4 - b3) / ((b4 + (2.4* b3) + 1))"}
                    dbmath['bandmath'].update(devi2)
                elif items=='sr':
                    dsr={"pixel_type": "32R","sr": "(b4/b3)"}
                    dbmath['bandmath'].update(dsr)
                elif items=='msavi2':
                    dmsavi2={"pixel_type": "32R", "msavi2": "(2 * b4 - ((2 * b4 + 1) ** 2 - 8 * (b4 - b3)) ** 0.5) / 2"}
                    dbmath['bandmath'].update(dmsavi2)
                elif items=='compression':
                    dbundle['tools'].append(dtiff)
            #dbundle[]
    for key,value in kwargs.items():
        if key=='boundary' and value!=None:
                for items in k['tools']:
                    if items.get('clip'):
                        try:
                            if value.endswith('.geojson'):
                                with open(value) as aoi:
                                    aoi_resp = json.load(aoi)
                                    for things in aoi_resp['features']:
                                        ovall.append(things['geometry']['coordinates'])
                                #print(list_depth(ovall))
                                if len(ovall)>1:
                                    aoi_geom=ovall
                                    items['clip']['aoi']['coordinates']=aoi_geom
                                    temp=shape(items.get('clip')['aoi'])
                                    if not temp.is_valid:
                                        print('Solving shapely self intersection for multipolygon')
                                        temp=temp.buffer(0)
                                        geojson_string = json.dumps(shapely.geometry.mapping(temp))
                                        items['clip']['aoi']['coordinates']=json.loads(geojson_string)['coordinates']
                                        ft = {"type": "Feature","geometry": {"type": "Polygon","coordinates": []},"properties": {}}
                                        ft['geometry']['coordinates']=json.loads(geojson_string)['coordinates']
                                        print('Total number of vertices in geometry: '+str(vertexcount(ft)))
                                        if int(vertexcount(ft))>500:
                                            print('Simplifying geometry since Ordersv2 will only accept upto 500 vertex points')
                                            b= vw.simplify_feature(ft, number=495)
                                            print('Total number of vertices in simplified geometry: '+str(vertexcount(b))+'\n')
                                            items['clip']['aoi']['coordinates']=b['geometry']['coordinates']
                                            temp=shape(b['geometry'])
                                            if not temp.is_valid:
                                                temp=temp.buffer(0)
                                                geojson_string = json.dumps(shapely.geometry.mapping(temp))
                                                items['clip']['aoi']['coordinates']=json.loads(geojson_string)['coordinates']
                                            elif temp.is_valid:
                                                items['clip']['aoi']['coordinates']=b['geometry']['coordinates']
                                else:
                                    if list_depth(ovall)==0:
                                        aoi_geom = ovall
                                        items['clip']['aoi']['type']="Polygon"
                                        items['clip']['aoi']['coordinates']=aoi_geom
                                    elif list_depth(ovall)==1:
                                        aoi_geom = ovall[0]
                                        items['clip']['aoi']['type']="Polygon"
                                        items['clip']['aoi']['coordinates']=aoi_geom
                                    elif list_depth(ovall)==2:
                                        aoi_geom = ovall[0][0]
                                        items['clip']['aoi']['type']="Polygon"
                                        items['clip']['aoi']['coordinates']=aoi_geom
                                    else:
                                        print('Please check GeoJSON: Could not parse coordinates')
                            elif value.endswith('.json'):
                                with open (value) as aoi:
                                    aoi_resp=json.load(aoi)
                                    items['clip']['aoi']['coordinates']=aoi_resp['config'][0]['config']['coordinates']
                            elif value.endswith('.kml'):
                                getcoord=kml2coord(value)
                                items['clip']['aoi']['coordinates']=getcoord
                        except Exception as e:
                            #print(e)
                            print('Could not parse geometry')
        #         #print(e)
    for key,value in kwargs.items():
        if key=='aws' and value!=None:
            with open(value, 'r') as ymlfile:
                cfg = yaml.load(ymlfile)
                for section in cfg:
                    k['delivery']['amazon_s3']['bucket']=cfg['amazon_s3']['bucket']
                    k['delivery']['amazon_s3']['aws_region']=cfg['amazon_s3']['aws_region']
                    k['delivery']['amazon_s3']['aws_access_key_id']=cfg['amazon_s3']['aws_access_key_id']
                    k['delivery']['amazon_s3']['aws_secret_access_key']=cfg['amazon_s3']['aws_secret_access_key']
                    k['delivery']['amazon_s3']['path_prefix']=cfg['amazon_s3']['path_prefix']
    for key,value in kwargs.items():
        if key=='azure' and value!=None:
            with open(value, 'r') as ymlfile:
                cfg = yaml.load(ymlfile)
                for section in cfg:
                    k['delivery']['azure_blob_storage']['account']=cfg['azure']['account']
                    k['delivery']['azure_blob_storage']['container']=cfg['azure']['container']
                    k['delivery']['azure_blob_storage']['sas_token']=cfg['azure']['sas_token']
                    k['delivery']['azure_blob_storage']['storage_endpoint_suffix']=cfg['azure']['storage_endpoint_suffix']
                    k['delivery']['azure_blob_storage']['path_prefix']=cfg['azure']['path_prefix']
    for key,value in kwargs.items():
        if key=='gcs' and value!=None:
            with open(value, 'r') as ymlfile:
                cfg = yaml.load(ymlfile)
                for section in cfg:
                    k['delivery']['google_cloud_storage']['bucket']=cfg['gcs']['bucket']
                    k['delivery']['google_cloud_storage']['credentials']=cfg['gcs']['credentials']
                    k['delivery']['google_cloud_storage']['path_prefix']=cfg['gcs']['path_prefix']
    for key,value in kwargs.items():
        if key=='compression' and value!=None:
            for items in k['tools']:
                if items.get('tiff_optimize'):
                    items['tiff_optimize']['compression']=value
    for key,value in kwargs.items():
        if key=='kernel' and value!=None:
            for items in k['tools']:
                if items.get('reproject'):
                    items['reproject']['kernel']=value
    for key,value in kwargs.items():
        if key=='projection' and value!=None:
            for items in k['tools']:
                if items.get('reproject'):
                    items['reproject']['projection']=value

    bnames=[]
    for items in dbmath['bandmath']:
        if items !='pixel_type':
            bnames.append(items)

    rg=len(bnames)
    if rg<6:
        dck=['b'+str(el) for el in range(1,rg+1)] #get serialized bands
        plist=[list(pair) for pair in zip(dck, bnames)]
        x.field_names = ["Band Number", "Band Name"]
        for items in plist:
            i=items[0]
            f=items[1]
            x.add_row([i,f])
            dbmath['bandmath'][i]=dbmath['bandmath'].pop(f)
    else:
        print('You can only use upto 5 bands')
        sys.exit()

    if len(dbmath['bandmath'])>0:
        k['tools'].append(dbmath)
    json_data = json.dumps(k)
    payload = json_data
    if len(bnames):
        print('\n')
        print(x)
        print('\n')

    # print('')
    #print(dbmath)

    #print(payload)
    ordname=k['name']
    payload=payload.replace("Explorer_{{name}}.zip",ordname+'_'+str(pendulum.now()).split("T")[0]+".zip")
    headers = {'content-type': 'application/json',
               'cache-control': 'no-cache'}
    response = requests.request('POST', url, data=payload, headers=headers,
                                auth=(PL_API_KEY, ''))
    if response.status_code==202:
        content = response.json()
        try:
            clipboard.copy(str(url) + '/' + str(content['id']))
            print('Order created at '+str(url) + '/' + str(content['id']+' and url copied to clipboard'))
            return (str(url) + '/' + str(content['id']))
        except Exception:
            print('Headless Setup: Order created at '+str(url) + '/' + str(content['id']))
    elif response.status_code==400:
        print('Failed with response: Bad request')
        print(response.json()['general'][0]['message'])
    elif response.status_code==401:
        print('Failed with response: Forbidden')
    elif response.status_code==409:
        print('Failed with response: MaxConcurrency')
    else:
        print(response.text)
Ejemplo n.º 6
0
def geosimple(inp, output, num):
    try:
        import fiona
        shape = fiona.open(inp)
        with open(inp) as aoi:
            aoi_resp = json.load(aoi)
            if list_depth(
                    aoi_resp['features'][0]['geometry']['coordinates']) == 0:
                print('Number of current vertices ' + str(
                    len(aoi_resp['features'][0]['geometry']['coordinates'][0]))
                      )
            elif list_depth(
                    aoi_resp['features'][0]['geometry']['coordinates']) == 1:
                print('Number of current vertices ' + str(
                    len(aoi_resp['features'][0]['geometry']['coordinates'][0]
                        [0])))
            else:
                print('Please check GeoJSON: Could not parse coordinates')
        with fiona.Env():
            with fiona.open(inp, 'r') as src:
                with fiona.open(output,
                                'w',
                                schema=src.schema,
                                driver=src.driver,
                                crs=src.crs) as sink:
                    for f in src:
                        sink.write(vw.simplify_feature(f, number=num))
            print('Write Completed to: ' + str(output))
    except ImportError:
        with open(inp) as aoi:
            aoi_resp = json.load(aoi)
            if list_depth(
                    aoi_resp['features'][0]['geometry']['coordinates']) == 0:
                aoi_geom = aoi_resp['features'][0]['geometry']['coordinates']
                print('Number of current vertices ' + str(
                    len(aoi_resp['features'][0]['geometry']['coordinates'][0]))
                      )
            elif list_depth(
                    aoi_resp['features'][0]['geometry']['coordinates']) == 1:
                aoi_geom = aoi_resp['features'][0]['geometry']['coordinates'][
                    0]
                print('Number of current vertices ' + str(
                    len(aoi_resp['features'][0]['geometry']['coordinates'][0]
                        [0])))
            else:
                print('Please check GeoJSON: Could not parse coordinates')
        ft = {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": []
            },
            "properties": {
                "vertex_count": num
            }
        }
        ft['geometry']['coordinates'] = aoi_geom
        #returns a copy of the feature, simplified (using number of vertices)
        b = vw.simplify_feature(ft, number=num)
        ft['geometry']['coordinates'] = b['geometry']['coordinates']
        with open(output, 'w') as g:
            json.dump(ft, g)
        print('Write Completed to: ' + str(output))
Ejemplo n.º 7
0
def order(**kwargs):
    for key, value in kwargs.items():
        if key == "name":
            dbundle["name"] = value
        if key == "sid":
            if value is not None:
                dbundle["subscription_id"] = int(value)
            else:
                dbundle.pop("subscription_id", None)
        if key == "item":
            dbundle["products"][0]["item_type"] = value
        if key == "asset":
            dbundle["products"][0]["product_bundle"] = value
        if key == "idlist":
            l = []
            if value.endswith(".csv"):
                with open(value) as f:
                    try:
                        reader = csv.reader(f)
                        for row in reader:
                            item_id = row[0]
                            if str(item_id.isalpha()) == "False":
                                l.append(item_id)
                    except Exception as e:
                        print("Issue with reading: " + str(value))
            elif value.endswith(".txt"):
                with open(value) as f:
                    for line in f:
                        item_id = line.strip()
                        l.append(item_id)
            dbundle["products"][0]["item_ids"] = l
    k = dbundle
    for key, value in kwargs.items():
        if key == "op" and value != None:
            for items in value:
                if items == "clip":
                    dbundle["tools"].append(dclip)
                elif items == "toar":
                    dbundle["tools"].append(dtoar)
                elif items == "harmonize":
                    dbundle["tools"].append(dharmonize)
                elif items == "coreg":
                    dbundle["tools"].append(dcoreg)
                elif items == "format":
                    dbundle["tools"].append(dformat)
                elif items == "zip":
                    dbundle["delivery"].update(dzip["delivery"])
                elif items == "zipall":
                    dbundle["delivery"].update(dszip["delivery"])
                elif items == "email":
                    dbundle.update(demail)
                elif items == "aws":
                    dbundle["delivery"].update(daws["delivery"])
                elif items == "azure":
                    dbundle["delivery"].update(dazure["delivery"])
                elif items == "gcs":
                    dbundle["delivery"].update(dgcs["delivery"])
                elif items == "gee":
                    dbundle["delivery"].update(dgee["delivery"])
                elif items == "composite":
                    dbundle["tools"].append(dcomposite)
                elif items == "projection":
                    dbundle["tools"].append(dreproject)
                elif items == "ndvi":
                    dndvi = {"pixel_type": "32R", "ndvi": "(b4 - b3) / (b4+b3)"}
                    dbmath["bandmath"].update(dndvi)
                elif items == "gndvi":
                    dgndvi = {"pixel_type": "32R", "gndvi": "(b4 - b2) / (b4+b2)"}
                    dbmath["bandmath"].update(dgndvi)
                elif items == "ndwi":
                    dndwi = {"pixel_type": "32R", "ndwi": "(b2 - b4) / (b4+b2)"}
                    dbmath["bandmath"].update(dndwi)
                elif items == "bndvi":
                    bndvi = {"pixel_type": "32R", "bndvi": "(b4-b1)/(b4+b1)"}
                    dbmath["bandmath"].update(bndvi)
                elif items == "tvi":
                    dtvi = {"pixel_type": "32R", "tvi": "((b4-b3)/(b4+b3)+0.5) ** 0.5"}
                    dbmath["bandmath"].update(dtvi)
                elif items == "osavi":
                    dosavi = {
                        "pixel_type": "32R",
                        "osavi": "1.16 * (b4-b3)/(b4+b3+0.16)",
                    }
                    dbmath["bandmath"].update(dosavi)
                elif items == "evi2":
                    devi2 = {
                        "pixel_type": "32R",
                        "evi2": "2.5 * (b4 - b3) / ((b4 + (2.4* b3) + 1))",
                    }
                    dbmath["bandmath"].update(devi2)
                elif items == "sr":
                    dsr = {"pixel_type": "32R", "sr": "(b4/b3)"}
                    dbmath["bandmath"].update(dsr)
                elif items == "msavi2":
                    dmsavi2 = {
                        "pixel_type": "32R",
                        "msavi2": "(2 * b4 - ((2 * b4 + 1) ** 2 - 8 * (b4 - b3)) ** 0.5) / 2",
                    }
                    dbmath["bandmath"].update(dmsavi2)
                elif items == "compression":
                    dbundle["tools"].append(dtiff)
            # dbundle[]
    for key, value in kwargs.items():
        if key == "boundary" and value != None:
            for items in k["tools"]:
                if items.get("clip"):
                    try:
                        if value.endswith(".geojson"):
                            aoi_resp = multipoly(value)
                            try:
                                for things in aoi_resp["features"]:
                                    ovall.append(things["geometry"]["coordinates"])
                            except Exception:
                                for things in json.loads(aoi_resp)["features"]:
                                    ovall.append(things["geometry"]["coordinates"])

                            aoi_geom = ovall
                            if (
                                list_depth(ovall) == 2
                                and json.loads(aoi_resp)["features"][0]["geometry"][
                                    "type"
                                ]
                                == "MultiPolygon"
                            ):
                                items["clip"]["aoi"]["coordinates"] = aoi_geom[0]
                            elif (
                                list_depth(ovall) == 2
                                and json.loads(aoi_resp)["features"][0]["geometry"][
                                    "type"
                                ]
                                != "MultiPolygon"
                            ):
                                aoi_geom = ovall[0][0]
                                items["clip"]["aoi"]["type"] = "Polygon"
                                items["clip"]["aoi"]["coordinates"] = aoi_geom
                            elif list_depth(ovall) == 1:
                                aoi_geom = ovall[0]
                                items["clip"]["aoi"]["type"] = "Polygon"
                                items["clip"]["aoi"]["coordinates"] = aoi_geom
                            elif list_depth(ovall) == 0:
                                aoi_geom = ovall
                                items["clip"]["aoi"]["type"] = "Polygon"
                                items["clip"]["aoi"]["coordinates"] = aoi_geom
                            else:
                                print(
                                    "Please check GeoJSON: Could not parse coordinates"
                                )
                            try:
                                temp = shape(items.get("clip")["aoi"])
                            except Exception as e:
                                print(e)
                            if not temp.is_valid:
                                print(
                                    "Solving shapely self intersection for multipolygon"
                                )
                                temp = temp.buffer(0)
                                geojson_string = json.dumps(
                                    shapely.geometry.mapping(temp)
                                )
                                items["clip"]["aoi"]["coordinates"] = json.loads(
                                    geojson_string
                                )["coordinates"]
                                ft = {
                                    "type": "Feature",
                                    "geometry": {"type": "Polygon", "coordinates": []},
                                    "properties": {},
                                }
                                ft["geometry"]["coordinates"] = json.loads(
                                    geojson_string
                                )["coordinates"]
                                print(
                                    "Total number of vertices in geometry: "
                                    + str(vertexcount(ft))
                                )
                                if int(vertexcount(ft)) > 500:
                                    print(
                                        "Simplifying geometry since Ordersv2 will only accept upto 500 vertex points"
                                    )
                                    b = vw.simplify_feature(ft, number=495)
                                    print(
                                        "Total number of vertices in simplified geometry: "
                                        + str(vertexcount(b))
                                        + "\n"
                                    )
                                    items["clip"]["aoi"]["coordinates"] = b["geometry"][
                                        "coordinates"
                                    ]
                                    temp = shape(b["geometry"])
                                    if not temp.is_valid:
                                        temp = temp.buffer(0)
                                        geojson_string = json.dumps(
                                            shapely.geometry.mapping(temp)
                                        )
                                        items["clip"]["aoi"][
                                            "coordinates"
                                        ] = json.loads(geojson_string)["coordinates"]
                                    elif temp.is_valid:
                                        items["clip"]["aoi"]["coordinates"] = b[
                                            "geometry"
                                        ]["coordinates"]
                        elif value.endswith(".json"):
                            with open(value) as aoi:
                                aoi_resp = json.load(aoi)
                                items["clip"]["aoi"]["coordinates"] = aoi_resp[
                                    "config"
                                ][0]["config"]["coordinates"]
                        elif value.endswith(".kml"):
                            getcoord = kml2coord(value)
                            items["clip"]["aoi"]["coordinates"] = getcoord
                    except Exception as e:
                        print(e)
                        # print('Could not parse geometry')
        #         #print(e)
    for key, value in kwargs.items():
        if key == "aws" and value != None:
            with open(value, "r") as ymlfile:
                cfg = yaml.load(ymlfile, Loader=yaml.FullLoader)
                for section in cfg:
                    k["delivery"]["amazon_s3"]["bucket"] = cfg["amazon_s3"]["bucket"]
                    k["delivery"]["amazon_s3"]["aws_region"] = cfg["amazon_s3"][
                        "aws_region"
                    ]
                    k["delivery"]["amazon_s3"]["aws_access_key_id"] = cfg["amazon_s3"][
                        "aws_access_key_id"
                    ]
                    k["delivery"]["amazon_s3"]["aws_secret_access_key"] = cfg[
                        "amazon_s3"
                    ]["aws_secret_access_key"]
                    k["delivery"]["amazon_s3"]["path_prefix"] = cfg["amazon_s3"][
                        "path_prefix"
                    ]
    for key, value in kwargs.items():
        if key == "azure" and value != None:
            with open(value, "r") as ymlfile:
                cfg = yaml.load(ymlfile, Loader=yaml.FullLoader)
                for section in cfg:
                    k["delivery"]["azure_blob_storage"]["account"] = cfg["azure"][
                        "account"
                    ]
                    k["delivery"]["azure_blob_storage"]["container"] = cfg["azure"][
                        "container"
                    ]
                    k["delivery"]["azure_blob_storage"]["sas_token"] = cfg["azure"][
                        "sas_token"
                    ]
                    k["delivery"]["azure_blob_storage"][
                        "storage_endpoint_suffix"
                    ] = cfg["azure"]["storage_endpoint_suffix"]
                    k["delivery"]["azure_blob_storage"]["path_prefix"] = cfg["azure"][
                        "path_prefix"
                    ]
    for key, value in kwargs.items():
        if key == "gcs" and value != None:
            with open(value, "r") as ymlfile:
                cfg = yaml.load(ymlfile, Loader=yaml.FullLoader)
                for section in cfg:
                    k["delivery"]["google_cloud_storage"]["bucket"] = cfg["gcs"][
                        "bucket"
                    ]
                    k["delivery"]["google_cloud_storage"]["credentials"] = cfg["gcs"][
                        "credentials"
                    ]
                    k["delivery"]["google_cloud_storage"]["path_prefix"] = cfg["gcs"][
                        "path_prefix"
                    ]
    for key, value in kwargs.items():
        if key == "gee" and value != None:
            project = value.split(',')[0]
            collection = value.split(',')[-1]
            k["delivery"]["google_earth_engine"]["project"] = project
            k["delivery"]["google_earth_engine"]["collection"] = collection

    for key, value in kwargs.items():
        if key == "compression" and value != None:
            for items in k["tools"]:
                if items.get("tiff_optimize"):
                    items["tiff_optimize"]["compression"] = value
    for key, value in kwargs.items():
        if key == "kernel" and value != None:
            for items in k["tools"]:
                if items.get("reproject"):
                    items["reproject"]["kernel"] = value
    for key, value in kwargs.items():
        if key == "projection" and value != None:
            for items in k["tools"]:
                if items.get("reproject"):
                    items["reproject"]["projection"] = value
    for key, value in kwargs.items():
        if key == "anchor" and value != None:
            for items in k["tools"]:
                if items.get("coregister"):
                    items["coregister"]["anchor_item"] = value
    for key, value in kwargs.items():
        if key == "format" and value != None:
            for items in k["tools"]:
                if items.get("file_format"):
                    items["file_format"]["format"] = value
    bnames = []
    for items in dbmath["bandmath"]:
        if items != "pixel_type":
            bnames.append(items)

    rg = len(bnames)
    if rg < 6:
        dck = ["b" + str(el) for el in range(1, rg + 1)]  # get serialized bands
        plist = [list(pair) for pair in zip(dck, bnames)]
        x.field_names = ["Band Number", "Band Name"]
        for items in plist:
            i = items[0]
            f = items[1]
            x.add_row([i, f])
            dbmath["bandmath"][i] = dbmath["bandmath"].pop(f)
    else:
        print("You can only use upto 5 bands")
        sys.exit()

    if len(dbmath["bandmath"]) > 0:
        k["tools"].append(dbmath)
    json_data = json.dumps(k)
    payload = json_data
    if len(bnames):
        print("\n")
        print(x)
        print("\n")

    # print('')
    # print(dbmath)

    #print(payload)
    ordname = k["name"]
    payload = payload.replace(
        "{{name}}_{{order_id}}.zip",
        "{{name}}_{{order_id}}_" + str(date.today()) + ".zip",
    )
    # print(payload)
    headers = {"content-type": "application/json", "cache-control": "no-cache"}
    response = requests.request(
        "POST", url, data=payload, headers=headers, auth=(PL_API_KEY, "")
    )
    if response.status_code == 202:
        content = response.json()
        try:
            clipboard.copy(str(url) + "/" + str(content["id"]))
            print(
                "Order created at "
                + str(url)
                + "/"
                + str(content["id"] + " and url copied to clipboard")
            )
            return str(url) + "/" + str(content["id"])
        except Exception:
            print(
                "Headless Setup: Order created at "
                + str(url)
                + "/"
                + str(content["id"])
            )
    elif response.status_code == 400:
        print("Failed with response: Bad request")
        print(response.json()["general"][0]["message"])
    elif response.status_code == 401:
        print("Failed with response: Forbidden")
    elif response.status_code == 409:
        print("Failed with response: MaxConcurrency")
    else:
        print(response.text)
Ejemplo n.º 8
0
def geosimple(inp, output, num):
    if int(num) >= 500:
        print("\n" + "Note: Ordersv2 will only accept upto 500 vertex points")
    print("")
    try:
        import fiona

        shape = fiona.open(inp)
        with open(inp) as aoi:
            aoi_resp = json.load(aoi)
            if list_depth(
                    aoi_resp["features"][0]["geometry"]["coordinates"]) == 0:
                print("Number of current vertices: " + str(
                    len(aoi_resp["features"][0]["geometry"]["coordinates"][0]))
                      )
            elif list_depth(
                    aoi_resp["features"][0]["geometry"]["coordinates"]) == 1:
                print("Number of current vertices: " + str(
                    len(aoi_resp["features"][0]["geometry"]["coordinates"][0]
                        [0])))
            else:
                print("Please check GeoJSON: Could not parse coordinates")
        with fiona.Env():
            with fiona.open(inp, "r") as src:
                with fiona.open(output,
                                "w",
                                schema=src.schema,
                                driver=src.driver,
                                crs=src.crs) as sink:
                    for f in src:
                        if int(num) == 500:
                            num = int(num) - 2
                        else:
                            num = int(num) - 1
                        sink.write(vw.simplify_feature(f, number=num))
            print("Write Completed to: " + str(output))
            vertexcount(output)
    except ImportError:
        with open(inp) as aoi:
            aoi_resp = json.load(aoi)
            if list_depth(
                    aoi_resp["features"][0]["geometry"]["coordinates"]) == 0:
                aoi_geom = aoi_resp["features"][0]["geometry"]["coordinates"]
                print("Number of current vertices: " + str(
                    len(aoi_resp["features"][0]["geometry"]["coordinates"][0]))
                      )
            elif list_depth(
                    aoi_resp["features"][0]["geometry"]["coordinates"]) == 1:
                aoi_geom = aoi_resp["features"][0]["geometry"]["coordinates"][
                    0]
                print("Number of current vertices: " + str(
                    len(aoi_resp["features"][0]["geometry"]["coordinates"][0]
                        [0])))
            else:
                print("Please check GeoJSON: Could not parse coordinates")
        ft = {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": []
            },
            "properties": {
                "vertex_count": num
            },
        }
        ft["geometry"]["coordinates"] = aoi_geom
        # returns a copy of the feature, simplified (using number of vertices)
        if int(num) == 500:
            num = int(num) - 2
        else:
            num = int(num) - 1
        b = vw.simplify_feature(ft, number=num)
        ft["geometry"]["coordinates"] = b["geometry"]["coordinates"]
        with open(output, "w") as g:
            json.dump(ft, g)
        print("Write Completed to: " + str(output))
        vertexcount(output)
    except Exception as e:
        print(e)