[英]Geospatial data with azure cosmos db SQL API using python
我在 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 文档中所述: 链接
我现在正在尝试查询一些数据,但没有获得任何结果,我想获得多边形内的点,如下所示:
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)
我的示例文件:
输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.