File: sample_search_inside_geometry_async.py

package info (click to toggle)
python-azure 20230112%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 749,544 kB
  • sloc: python: 6,815,827; javascript: 287; makefile: 195; xml: 109; sh: 105
file content (121 lines) | stat: -rw-r--r-- 4,177 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# coding: utf-8

# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

"""
FILE: sample_search_inside_geometry_async.py
DESCRIPTION:
    This sample demonstrates how to perform search inside geometry by given target such as `pizza` and multiple different geometry as input, which include GeoJson object and geo_interface property from other existing packages such as shapely.
USAGE:
    python sample_search_inside_geometry_async.py

    Set the environment variables with your own values before running the sample:
    - AZURE_SUBSCRIPTION_KEY - your subscription key
"""
import asyncio
import os

subscription_key = os.getenv("AZURE_SUBSCRIPTION_KEY")


async def search_inside_geometry():
    from azure.core.credentials import AzureKeyCredential
    from azure.maps.search import MapsSearchClient

    maps_search_client = MapsSearchClient(credential=AzureKeyCredential(subscription_key))


    geo_json_obj1 = {
        "type": "FeatureCollection",
        "features": [
            {
                "type": "Feature",
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [[
                        [-122.143035,47.653536],
                        [-122.187164,47.617556],
                        [-122.114981,47.570599],
                        [-122.132756,47.654009],
                        [-122.143035,47.653536]
                        ]]
                },
                "properties": {}
            },
            {
                "type": "Feature",
                "geometry": {
                    "type": "Point",
                    "coordinates": [-122.126986,47.639754]
                },
                "properties": {
                    "subType": "Circle",
                    "radius": 100
                }
            }
        ]
    }

    geo_json_obj2= {
        'type': 'Polygon',
        'coordinates': [
            [[-122.43576049804686, 37.7524152343544],
             [-122.43301391601562, 37.70660472542312],
             [-122.36434936523438, 37.712059855877314],
             [-122.43576049804686, 37.7524152343544]]
        ]
    }

    # This is the mock results we can get from 3rd party package `Shapely`
    #
    # from shapely.geometry import Polygon
    #
    # data = Polygon([
    #     [-122.43576049804686, 37.7524152343544],
    #     [-122.43301391601562, 37.70660472542312],
    #     [-122.36434936523438, 37.712059855877314],
    #     [-122.43576049804686, 37.7524152343544]
    # ]).__geo_interface__
    #
    geo_obj_interface = {
        'type': 'Polygon',
        'coordinates': ((
            (-122.43576049804686, 37.7524152343544),
            (-122.43301391601562, 37.70660472542312),
            (-122.36434936523438, 37.712059855877314),
            (-122.43576049804686, 37.7524152343544)),
        )
    }

    result1 = maps_search_client.search_inside_geometry(
        query="pizza",
        geometry=geo_json_obj1
    )
    print("Search inside geometry with standard GeoJson obejct as input, FeatureCollection:")
    print(result1.__dict__)

    print("Result 2 and result 3 should be the same: ")
    print("__________________________________________")
    result2 = maps_search_client.search_inside_geometry(
        query="pizza",
        geometry=geo_json_obj2
    )
    print("Search inside geometry with standard GeoJson object as input, Polygon as result 2:")
    print(result2.__dict__)
    print(f'Id of the first result item of result 2: {result2.results[0].id}')

    result3 = maps_search_client.search_inside_geometry(
        query="pizza",
        geometry=geo_obj_interface
    )
    print("Search inside geometry with Polygon from third party library `shapely` with geo_interface as result 3:")
    print(result2.__dict__)
    print(f'Id of the first result item of result 3: {result3.results[0].id}')


if __name__ == '__main__':
    asyncio.run(search_inside_geometry())