$geoIntersects - Amazon DocumentDB

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

$geoIntersects

Amazon DocumentDB의 $geoIntersects 연산자는 지리 공간 데이터가 지정된 GeoJSON 객체와 교차하는 문서를 찾는 데 사용됩니다. 이 연산자는 다각형 또는 다중 다각형과 같은 지정된 지리적 형태와의 공간 관계를 기반으로 문서를 식별해야 하는 애플리케이션에 유용합니다.

파라미터

  • $geometry: 교차를 확인할 셰이프를 나타내는 GeoJSON 객체입니다. 지원되는 GeoJSON 객체 유형은 Point, LineStringPolygon, 및 입니다MultiPolygon.

예제(MongoDB 쉘)

다음 예제에서는 $geoIntersects 연산자를 사용하여 Amazon DocumentDB에서 지정된 좌표 집합의 상태 이름을 찾는 방법을 보여줍니다.

샘플 문서 생성

db.states.insertMany([ { "name": "New York", "loc": { "type": "Polygon", "coordinates": [[ [-74.25909423828125, 40.47556838210948], [-73.70819091796875, 40.47556838210948], [-73.70819091796875, 41.31342607582222], [-74.25909423828125, 41.31342607582222], [-74.25909423828125, 40.47556838210948] ]] } }, { "name": "California", "loc": { "type": "Polygon", "coordinates": [[ [-124.4091796875, 32.56456771381587], [-114.5458984375, 32.56456771381587], [-114.5458984375, 42.00964153424558], [-124.4091796875, 42.00964153424558], [-124.4091796875, 32.56456771381587] ]] } } ]);

쿼리 예제

var location = [-73.965355, 40.782865]; db.states.find({ "loc": { "$geoIntersects": { "$geometry": { "type": "Point", "coordinates": location } } } }, { "name": 1 });

출력

{ "_id" : ObjectId("536b0a143004b15885c91a2c"), "name" : "New York" }

코드 예제

$geoIntersects 명령을 사용하기 위한 코드 예제를 보려면 사용하려는 언어의 탭을 선택합니다.

Node.js
const { MongoClient } = require('mongodb'); async function findStateByGeoIntersects(longitude, latitude) { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const collection = db.collection('states'); const query = { loc: { $geoIntersects: { $geometry: { type: 'Point', coordinates: [longitude, latitude] } } } }; const projection = { _id: 0, name: 1 }; const document = await collection.findOne(query, { projection }); await client.close(); if (document) { return document.name; } else { throw new Error('The geo location you entered was not found in the United States!'); } }
Python
from pymongo import MongoClient def find_state_by_geointersects(longitude, latitude): try: client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client.test collection_states = db.states query_geointersects = { "loc": { "$geoIntersects": { "$geometry": { "type": "Point", "coordinates": [longitude, latitude] } } } } document = collection_states.find_one(query_geointersects, projection={ "_id": 0, "name": 1 }) if document is not None: state_name = document['name'] return state_name else: raise Exception("The geo location you entered was not found in the United States!") except Exception as e: print('Exception in geoIntersects: {}'.format(e)) raise finally: if client is not None: client.close()