大疆智图api接入python跑图

1.第一步要去大疆智图开发者申请api,需要填公司信息,不然审核不通过。获取ak和sk后在下载2维实例数据,将它们配置在代码里就可以运行了。
python代码(我代码中只获取了result.tif,需要高程tif等其他结果可以自行修改):


import requests
import os
import oss2
import base64
import hashlib
import hmac
import json
import time
import threading


dji_app_key = "xxxxxxxxxx"#你申请的appkey
dji_secret_key = b"xxxxxxxxxxxxxxxxxxxxxxx"#你申请的secret_key
get_token_uri = "/terra-rescon-be/v2/store/obtain_token"
get_resource_uri = "/terra-rescon-be/v2/resources"
bind_files_uri = "/terra-rescon-be/v2/store/upload_callback"
create_job_uri = "/terra-rescon-be/v2/jobs"
start_job_uri = "/terra-rescon-be/v2/jobs/{jobuuid}/start"
get_job_status_uri = "/terra-rescon-be/v2/jobs/{jobuuid}"
get_files_uuid_uri = '/terra-rescon-be/v2/resources/{outputResourceUuid}'
get_file_url_uri = '/terra-rescon-be/v2/files/{fileuuid}'

host = "https://openapi-cn.dji.com"

method = "POST"
method_Get = "Get"

#1获取token   2上传素材   3获取resource   4关联文件    5创建job   6启动job   7查询job状态,等待成功   8查询文件信息,下载文件
def getToken():
    uri = get_token_uri
    url = host + uri
    payload = {
    }

    digest = (
        lambda x: base64.b64encode(hashlib.sha256(x.encode("utf-8")).digest()).decode(
            "utf-8"
        )
    )(json.dumps(payload))

    gmt_time = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(time.time()))
    signing_string = f"date: {gmt_time}\n@request-target: {method.lower()} {uri}\ndigest: SHA-256={digest}"
    signature = (
        lambda secret, x: base64.b64encode(
            hmac.new(secret, x.encode("utf-8"), hashlib.sha256).digest()
        ).decode("utf-8")
    )(dji_secret_key, signing_string)

    headers = {
        "Content-Type": "application/json",
        "Date": gmt_time,
        "Digest": f"SHA-256={digest}",
        "Authorization": f'hmac username="{dji_app_key}", algorithm="hmac-sha256", headers="date @request-target digest", signature="{signature}"',
    }


    r = requests.post(url, json=payload, headers=headers)
    res = r.content.decode()
    resjson = json.loads(res)
    status = resjson['result']
    if status['msg'] == 'success':
        return resjson['data']
    else:
        return 'Create Token Error'
    # {'cloudName': 'ali_oss', 'accessKeyID': 'STS.NT1hjfYwZoKs8YwyEsifm6exK',
    #  'secretAccessKey': '5iJiBrWWsUxkRgwySiuyyJoBkrdVaP9kLyLp9oouJKR2',
    #  'sessionToken': 'CAIStwN1q6Ft5B2yfSjIr5eEI9DStKh72ImYOn/GnUUmZelB2aDTqTz2IHFNeHRoA+wXv/0xnmlW7fkblqpoV4Qdjosoi3wpvPpt6gqET9frKKXXhOV2FfTHdEGXDxnkpviwB8zyUNLafNq0dlnAjVUd6LDmdDKkLXPHVJqSksxfc8gwVAu1ZiYkYdBNPVlNpdM9P3ncPurPVxnxmTj0DVF0ggNmlXgdmaOk2Z+14B3EkHjnzvMUv43rPqW8a85lMO2dOrXT5uFtcbfb2yN98gVD8LwM7JZJ4jDapNqQcV8z5xyNKLjT6cY9bl07NKgoXrJJtuL10Pd1//TJkIiwlEpGYLlYXS+aGI74zo6YErr2c4dmfuy+MC+W24GDOMGt7A96Jm4eLERHcYJ/enEsUlkgFm2Ab/P7pwuPaAv6Qe3bjvpmjcotngqz8IrNZXr3GuXGiHZEYsNsNhh0bkVIgVaMKPFWL1Z+FHp8HKuQQIlJaxRTlLjlpwW6VFc7nygL4qWkO6KH4/5FMNukActcmo0Gf9FGoS42Qk7tQvekhwIMbmtpBKxJlaDmMpKk8/qfx+GecTJxvw+yH5QxGoABKcvKVFVAWDkrDWCfdw/QjM917538aNsrNFQI6q2SgYIhf+iBiOFXrHhdYsycqJ+m6OBkaDhFevtkDE88rOA4DqNSeMwGQ2UkHfR/oxlpSol+l51kpPU0P+WkWjmZ66q6AXAMtdWSQhFL9b17/b1FhRoi0sihZ41oRFLXCAt5ZpcgAA==',
    #  'region': 'oss-cn-hangzhou', 'cloudBucketName': 'hz-terra-be-prod',
    #  'callbackParam': 'ek1zZ3NpZTg5ZXRDVjA3WjBnYm1pMDlIRVI0S0JVSzMzMXplanNLU2hqVEdlSW5TeTNTM0w4ZzROV1FuMVk3b28xUUV2Ty9OcmJkTkN4Q0JMWnM5ZjJXZjVnVTZQN0xPenpFWHJmaWk5T243T3A0WUhMZmpsUXVmcXlta0JsVm9DSW5GOWpDbHplR1pEVmxPd3R0SDdHQVlwSExvZVN3enBTYTY0WmpXelNkeWhmT3NaaC9XbkNiN0J6VFgvOTg3',
    #  'storePath': '74c50efc-4f0f-4b17-97c2-c33a9df007d6/tmp/eb068f43-d442-4473-bd1b-6638480973e8/{fileName}',
    #  'expireTime': 1709157980}



#上传素材 , 将本地图像上传到大疆阿里云oss上
#access_key_id, access_key_secret, bucket_name, token, store_path,参数为getToken返回
#local_folder为选择本地素材文件夹
def upload_files(access_key_id, access_key_secret, bucket_name, token, store_path, local_folder):
    endpoint = 'http://oss-cn-hangzhou.aliyuncs.com'

    # 配置阿里云 OSS 访问信息
    # access_key_id = 'STS.NTGT9XRMuQkMfdMCC2Zhef6co'
    # access_key_secret = '9fB8frseuujCtzW9pAsV9NuhoUkX5QBCvNhn5UNBM7k6'
    # bucket_name = 'hz-terra-be-prod'
    # token = 'CAIShAR1q6Ft5B2yfSjIr5fyH4Psv5JU5qmmZEL8p0NnVudJifPIjTz2IHFNeHRoA+wXv/0xnmlW7fkblqpoV4Qd1aUYjHwpvPpt6gqET9frKKXXhOV2FfTHdEGXDxnkpviwB8zyUNLafNq0dlnAjVUd6LDmdDKkLXPHVJqSksxfc8gwVAu1ZiYkYdBNPVlNpdM9P3ncPurPVxnxmTj0DVF0ggNmlXgdmaOk2Z+14B3EkHjnzvMUv43rPqW8a85lMO2dOrXT5uFtcbfb2yN98gVD8LwM7JZJ4jDapNqQcV8z5xyNKLjT6cY9bl07NKgoXrJJtuL10Pd1//TJkIiwlEpGYLlYXS+aGI74zo6YErr2c4dmfuy+MC+W24GDOMGt7A96Jm4eLERDIdd+dHV5WVkgG2jRb/P9oQqPa1z/Eu2O3fpqgJB5kg3j8IvNZXr3GuXGiHZEYsNsNhh0bkVIgVaMKPFWL1Z+FHp8HKuQQIlJaxRTlLjlpwW6VFc7nygL4qWkO6KH4/5FMNukActcmo0Gf9FGoS42Qk7tQvekhwIMbmtpBKxJlaDmMpKk8/qfx+GecTJxvw+yH5Qxm1nz2MntIRs4RUWZjlazsMYJBNc/5hht4kHNYeo/Z6G+/Dv/BfJHhXIIbSU8EXrtvgGdXL5kkIQ2NLu83c17jWEAGFPMTBrDnWC21BQagAE/P++5y/XhAZ9Ky6AxPzdlX9+ip59b/+Om5tKYCipkLgEnDNehLDBmvZo2IrmLUEPi7sxNqxqiQT34TnSJZNwCmL+nbLzq0rAObB5Oz/fZA9AFXD/AGdQmWRpIFLdBwzYmJo9aeStWEk+YB9iw9c6Wm1hADARSCOLqmmi6qzRj+CAA'
    # store_path = '74c50efc-4f0f-4b17-97c2-c33a9df007d6/tmp/a2e76ba8-d91c-4212-a341-ce349bd50ce9/{fileName}'

    store_root_path = store_path[:store_path.rfind('/{fileName}')]

    # 创建 STS 访问凭证
    sts_auth = oss2.StsAuth(access_key_id, access_key_secret, token)

    # 创建 OSS 客户端
    bucket = oss2.Bucket(sts_auth, endpoint, bucket_name)
    bucket.timeout = 600

    # 本地文件夹路径
    # local_folder = r'D:\Two-Dimension-2D\5cm-2D-orthomosaic-53pic'

    # 遍历本地文件夹
    uploaded_files = []
    for root, dirs, files in os.walk(local_folder):
        for file_name in files:
            local_file_path = os.path.join(root, file_name)
            relative_path = os.path.relpath(os.path.join(root, file_name), local_folder)
            oss_file_path = os.path.join(store_root_path, relative_path)
            oss_file_path = oss_file_path.replace("\\", "/")
            put_result = bucket.put_object_from_file(oss_file_path, local_file_path)
            etag = put_result.etag
            # 保存结果的 etag,需要在 `关联文件` 步骤里用到
            print(f"Uploaded: {local_file_path} -> {oss_file_path}, etag: {etag}")
            container_file_path = relative_path.replace("\\", "/")
            uploaded_files.append({"name": container_file_path, "etag": etag, "checksum": etag})

    if uploaded_files:
        with open("./uploaded_files.json", "w") as f:
            json.dump(uploaded_files, f)



#获取resource
def getResource():
    uri = get_resource_uri
    url = host + uri
    payload = {
        "name": "test_resource",
        "type": "map"
    }
    digest = (
        lambda x: base64.b64encode(hashlib.sha256(x.encode("utf-8")).digest()).decode(
            "utf-8"
        )
    )(json.dumps(payload))

    gmt_time = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(time.time()))
    signing_string = f"date: {gmt_time}\n@request-target: {method.lower()} {uri}\ndigest: SHA-256={digest}"
    signature = (
        lambda secret, x: base64.b64encode(
            hmac.new(secret, x.encode("utf-8"), hashlib.sha256).digest()
        ).decode("utf-8")
    )(dji_secret_key, signing_string)

    headers = {
        "Content-Type": "application/json",
        "Date": gmt_time,
        "Digest": f"SHA-256={digest}",
        "Authorization": f'hmac username="{dji_app_key}", algorithm="hmac-sha256", headers="date @request-target digest", signature="{signature}"',
    }


    r = requests.post(url, json=payload, headers=headers)
    res = r.content.decode()
    resjson = json.loads(res)
    status = resjson['result']
    if status['msg'] == 'success':
        return resjson['data']
    else:
        return 'Create Token Error'
    # {'uuid': '16e9aefe-9d9d-453f-bb83-90e27d7f06ae'}






#####################关联文件
def bind_files(resource_uuid, callback_param):
    with open("./uploaded_files.json", "r") as f:
        uploaded_files = json.load(f)

    mini_batch = []
    for item in uploaded_files:
        mini_batch.append(item)
        if len(mini_batch) >= 50:
            bind_batch_files(mini_batch, resource_uuid, callback_param)
            mini_batch = []

    if mini_batch:
        bind_batch_files(mini_batch, resource_uuid, callback_param)

def bind_batch_files(files, resource_uuid, callback_param):
    uri = bind_files_uri
    url = host + uri

    payload = {
        "resourceUUID": resource_uuid,
        "callbackParam": callback_param,
        "files": files,
    }

    digest = (
        lambda x: base64.b64encode(hashlib.sha256(x.encode("utf-8")).digest()).decode(
            "utf-8"
        )
    )(json.dumps(payload))

    gmt_time = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(time.time()))
    signing_string = f"date: {gmt_time}\n@request-target: {method.lower()} {uri}\ndigest: SHA-256={digest}"
    signature = (
        lambda secret, x: base64.b64encode(
            hmac.new(secret, x.encode("utf-8"), hashlib.sha256).digest()
        ).decode("utf-8")
    )(dji_secret_key, signing_string)

    headers = {
        "Content-Type": "application/json",
        "Date": gmt_time,
        "Digest": f"SHA-256={digest}",
        "Authorization": f'hmac username="{dji_app_key}", algorithm="hmac-sha256", headers="date @request-target digest", signature="{signature}"',
    }

    print(signature)
    r = requests.post(url, json=payload, headers=headers)
    res = r.content.decode()
    resjson = json.loads(res)
    status = resjson['result']
    if status['msg'] == 'success':
        return resjson['data']
    else:
        return 'Create Token Error'
    # print(r.request.headers)
    # print(r.content)
    # print(r.headers)


#创建job
def createjob():
    uri = create_job_uri
    url = host + uri
    payload = {
        "name": "test_name"
    }
    digest = (
        lambda x: base64.b64encode(hashlib.sha256(x.encode("utf-8")).digest()).decode(
            "utf-8"
        )
    )(json.dumps(payload))

    gmt_time = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(time.time()))
    signing_string = f"date: {gmt_time}\n@request-target: {method.lower()} {uri}\ndigest: SHA-256={digest}"
    signature = (
        lambda secret, x: base64.b64encode(
            hmac.new(secret, x.encode("utf-8"), hashlib.sha256).digest()
        ).decode("utf-8")
    )(dji_secret_key, signing_string)

    headers = {
        "Content-Type": "application/json",
        "Date": gmt_time,
        "Digest": f"SHA-256={digest}",
        "Authorization": f'hmac username="{dji_app_key}", algorithm="hmac-sha256", headers="date @request-target digest", signature="{signature}"',
    }


    r = requests.post(url, json=payload, headers=headers)
    res = r.content.decode()
    resjson = json.loads(res)
    status = resjson['result']
    if status['msg'] == 'success':
        return resjson['data']
    else:
        return 'Create Token Error'
    # {'uuid': '16e9aefe-9d9d-453f-bb83-90e27d7f06ae'}





#启动job
def startjob(jobuuid, resourceuuid, epsgcode):
    uri = start_job_uri
    uri = uri.replace("{jobuuid}", jobuuid)
    url = host + uri
    payload = {
        "type": 14,
        "resourceUuid": resourceuuid,
        "parameters": "{\"parameter\":{\"map_mode\":1,\"quality_level\":1,\"output_geo_desc\":{\"cs_type\":\"GEO_CS\",\"geo_cs\":\""+epsgcode+"\",\"geo_cs_wkt\":\"\",\"override_vertical_cs\":\"\"}}}"
    }
    digest = (
        lambda x: base64.b64encode(hashlib.sha256(x.encode("utf-8")).digest()).decode(
            "utf-8"
        )
    )(json.dumps(payload))

    gmt_time = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(time.time()))
    signing_string = f"date: {gmt_time}\n@request-target: {method.lower()} {uri}\ndigest: SHA-256={digest}"
    signature = (
        lambda secret, x: base64.b64encode(
            hmac.new(secret, x.encode("utf-8"), hashlib.sha256).digest()
        ).decode("utf-8")
    )(dji_secret_key, signing_string)

    headers = {
        "Content-Type": "application/json",
        "Date": gmt_time,
        "Digest": f"SHA-256={digest}",
        "Authorization": f'hmac username="{dji_app_key}", algorithm="hmac-sha256", headers="date @request-target digest", signature="{signature}"',
    }


    r = requests.post(url, json=payload, headers=headers)
    res = r.content.decode()
    resjson = json.loads(res)
    status = resjson['result']
    if status['msg'] == 'success':
        return resjson['data']
    else:
        return 'Create Token Error'
    # {'uuid': '16e9aefe-9d9d-453f-bb83-90e27d7f06ae'}






#查询job状态
def getjob_status(jobuuid):
    uri = get_job_status_uri
    uri = uri.replace("{jobuuid}", jobuuid)
    url = host + uri
    payload = None
    digest = (
        lambda x: base64.b64encode(hashlib.sha256(x.encode("utf-8")).digest()).decode(
            "utf-8"
        )
    )

    gmt_time = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(time.time()))
    signing_string = f"date: {gmt_time}\n@request-target: {method_Get.lower()} {uri}\ndigest: SHA-256={digest}"
    signature = (
        lambda secret, x: base64.b64encode(
            hmac.new(secret, x.encode("utf-8"), hashlib.sha256).digest()
        ).decode("utf-8")
    )(dji_secret_key, signing_string)

    headers = {
        "Content-Type": "application/json",
        "Date": gmt_time,
        "Digest": f"SHA-256={digest}",
        "Authorization": f'hmac username="{dji_app_key}", algorithm="hmac-sha256", headers="date @request-target digest", signature="{signature}"',
    }


    r = requests.get(url, headers=headers)
    res = r.content.decode()
    resjson = json.loads(res)
    status = resjson['result']
    if status['msg'] == 'success':
        return resjson['data']
    else:
        return 'Create Token Error'
    # {'uuid': '16e9aefe-9d9d-453f-bb83-90e27d7f06ae'}




#获取返回文件uuid
def get_files_uuid(outputResourceUuid):
    uri = get_files_uuid_uri
    uri = uri.replace("{outputResourceUuid}", outputResourceUuid)
    url = host + uri
    payload = None
    digest = (
        lambda x: base64.b64encode(hashlib.sha256(x.encode("utf-8")).digest()).decode(
            "utf-8"
        )
    )

    gmt_time = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(time.time()))
    signing_string = f"date: {gmt_time}\n@request-target: {method_Get.lower()} {uri}\ndigest: SHA-256={digest}"
    signature = (
        lambda secret, x: base64.b64encode(
            hmac.new(secret, x.encode("utf-8"), hashlib.sha256).digest()
        ).decode("utf-8")
    )(dji_secret_key, signing_string)

    headers = {
        "Content-Type": "application/json",
        "Date": gmt_time,
        "Digest": f"SHA-256={digest}",
        "Authorization": f'hmac username="{dji_app_key}", algorithm="hmac-sha256", headers="date @request-target digest", signature="{signature}"',
    }


    r = requests.get(url, headers=headers)
    res = r.content.decode()
    resjson = json.loads(res)
    status = resjson['result']
    if status['msg'] == 'success':
        return resjson['data']
    else:
        return 'Create Token Error'
    # {'uuid': '16e9aefe-9d9d-453f-bb83-90e27d7f06ae'}



#根据文件uuid获取文件链接
def get_file_url(fileuuid):
    uri = get_file_url_uri
    uri = uri.replace("{fileuuid}", fileuuid)
    url = host + uri
    payload = None
    digest = (
        lambda x: base64.b64encode(hashlib.sha256(x.encode("utf-8")).digest()).decode(
            "utf-8"
        )
    )

    gmt_time = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(time.time()))
    signing_string = f"date: {gmt_time}\n@request-target: {method_Get.lower()} {uri}\ndigest: SHA-256={digest}"
    signature = (
        lambda secret, x: base64.b64encode(
            hmac.new(secret, x.encode("utf-8"), hashlib.sha256).digest()
        ).decode("utf-8")
    )(dji_secret_key, signing_string)

    headers = {
        "Content-Type": "application/json",
        "Date": gmt_time,
        "Digest": f"SHA-256={digest}",
        "Authorization": f'hmac username="{dji_app_key}", algorithm="hmac-sha256", headers="date @request-target digest", signature="{signature}"',
    }


    r = requests.get(url, headers=headers)
    res = r.content.decode()
    resjson = json.loads(res)
    status = resjson['result']
    if status['msg'] == 'success':
        return resjson['data']
    else:
        return 'Create Token Error'
    # {'uuid': '16e9aefe-9d9d-453f-bb83-90e27d7f06ae'}



def dji_api_function():
    #获取token
    token = getToken()
    if type(token) is str and token.endswith("Error"):
        return token
    access_key_id = token['accessKeyID']
    access_key_secret = token['secretAccessKey']
    session_token = token['sessionToken']
    store_path = token['storePath']
    bucket_name = token['cloudBucketName']
    callback_param = token['callbackParam']

    #上传本地素材
    #TODO local_folder
    upload_files(access_key_id, access_key_secret, bucket_name, session_token, store_path, 'D:/Two-Dimension-2D/5cm-2D-orthomosaic-53pic')

    #获取resource
    resource = getResource()
    if type(resource) is str and resource.endswith("Error"):
        return resource
    resourceuuid = resource['uuid']
    print("resourceuuid")
    print(resourceuuid)
    # 关联文件与resource
    bind_files(resourceuuid, callback_param)

    # 创建任务
    jobres = createjob()
    if type(jobres) is str and jobres.endswith("Error"):
        return jobres
    jobuuid = jobres['uuid']
    print("jobuuid")
    print(jobuuid)

    #启动任务,跑图
    startjob(jobuuid, resourceuuid, "EPSG:4548")

    # #获取结果uuid
    outputResourceUuid = ''
    jobsuccess = False
    while not jobsuccess:
        statusres = getjob_status(jobuuid)
        status = statusres['status']  # 6为完成
        if status == 6:
            outputResourceUuid = statusres['outputResourceUuid']
            jobsuccess = True
        else:
            time.sleep(30)  # 暂停 30 秒



    files_uuidres = get_files_uuid(outputResourceUuid)
    fileuuids = files_uuidres['fileUuids']
    if fileuuids is not None and len(fileuuids) > 0:
        fileuuids.reverse()
        for fuuid in fileuuids:
            fileurlres = get_file_url(fuuid)
            if type(fileurlres) is str and fileurlres.endswith("Error"):
                continue
            fileurl = fileurlres['url']
            if fileurl.endswith("result.tif"):
                print(fileurl)
                return fileurl



if __name__ == "__main__":
    res = dji_api_function()
    print(res)

    # jobuuid = '75023605-72e7-4f34-a86e-e4f7d4513af9'
    # res = getjob_status(jobuuid)
    # print(res)
    # res = get_file_url('e0c3ed8f-ff60-41d1-b280-6478c2077e7a')
    # print(res)

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值