gpt4 book ai didi

python - 使用 python 使用 azure cosmos db SQL API 处理地理空间数据

转载 作者:行者123 更新时间:2023-12-01 06:46:54 24 4
gpt4 key购买 nike

我在 azure cosmos db 中有 200 万条记录,¡。

我将它与带有 python 的 SQL API 一起使用。

我已将此记录改编为地理空间数据,如下示例所示:

{
"
"street": "Palmdale Station",
"year": "2015",
"incidentid": "D01DA82AE23D128799924FCF82B72908",
"id": "501",
"
"latitude": 34.58986,

"country": "United States",
"longitude": -89.61238680302355,
"_attachments": "attachments/",
"incidenttype": "Train Accident",
"_etag": "\"0900f6f1-0000-0100-0000-5dd6cf150000\""
}
{
"_self": "dbs/QwR-AA==/colls/QwR-AIt6sYk=/docs/QwR-AIt6sYnYUx0AAAAAAA==/",
"street": "Palmdale Station",
"year": 2015,
"incidentid": "D01DA82AE23D128799924FCF82B72908",
"id": "501",
"notifyrule": "Use Perimeter",
"city": "Palmdale",
"infoquality": "Media",
"severity": "Minor",
"incidentcategory": "Transportation",
"location": {
"type": "Point",
"coordinates": [
-89.61238680302355,
29.568987477308124
]
},
"latitude": 29.568987477308124,
"infosource": "Media",
"description": "One person was struck by an Antelope Valley Line Train near the Palmdale Station, located in the area of Clock Tower Plaza Dr E and Transportation Dr. AV Line tracks between Palmdale and Lancaster remain closed. This incident is closed.",
"_ts": 1575425976,
"activitystatus": "CLOSED",
"postal": "93550",
"createddated": "2015-12-30 21:46:00 EST",
"country": "United States",
"longitude": -118.1194,
"_attachments": "attachments/",
"incidenttype": "Train Accident",
"_etag": "\"13002b74-0000-0100-0000-5de717b80000\""
}
results_fake[0]['location'] = {'type': 'Point','coordinates':[results_fake[0]['longitude'],results_fake[0]['latitude']] }

如您所见,我创建了一个名为 location 的附加键,其中我编写了 type 和坐标:经度和纬度,如 microsoft azure 文档中所述: link

我现在正在尝试查询一些数据,但没有获得任何结果,我想获取像这样的多边形内的点:

query =""" SELECT a.id FROM " + container_id + " a
WHERE ST_WITHIN(a.location, {"type": "Polygon","coordinates": [
[[-90.0280152, 30.265521],
[-90.0335083,30.26315],
[-90.0032959,29.3532687],
[-89.1930542,29.3460904],
[-89.2067871,30.267892],
[-90.0280152,30.265521]]]})"""

for item in client.client_connection.QueryItems("dbs/" + database_id + "/colls/" + container_id,
query,
{'enableCrossPartitionQuery': True}):

print(json.dumps(item, indent=True))

但是我没有得到任何结果,我做错了什么?

最佳答案

您可以先在azure cosmos门户中测试您的sql。我测试了您的代码,请引用我的测试:

import pydocumentdb.document_client as document_client

config = {
'ENDPOINT': 'https://***.documents.azure.com:443/',
'MASTERKEY': '***'
};

# Initialize the Python DocumentDB client
client = document_client.DocumentClient(config['ENDPOINT'], {'masterKey': config['MASTERKEY']})

# use a SQL based query to get a bunch of documents
query = { 'query': "SELECT a.id FROM a WHERE ST_WITHIN(a.location, {'type': 'Polygon','coordinates': [[[-90.0280152, 30.265521],[-90.0335083,30.26315],[-90.0032959,29.3532687],[-89.1930542,29.3460904],[-89.2067871,30.267892],[-90.0280152,30.265521]]]})" }

options = {}
options['enableCrossPartitionQuery'] = True

result_iterable = client.QueryDocuments('dbs/db/colls/coll', query, options)

results = list(result_iterable);

print(results)

我的示例文档:

enter image description here

输出:

enter image description here

关于python - 使用 python 使用 azure cosmos db SQL API 处理地理空间数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59184694/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com