|
| 1 | +# coding: utf-8 |
| 2 | +import reearthcmsapi |
| 3 | +from reearthcmsapi.apis.tags import items_project_api |
| 4 | +from reearthcmsapi.model.model import Model |
| 5 | +from reearthcmsapi.model.versioned_item import VersionedItem |
| 6 | +from reearthcmsapi.model.asset_embedding import AssetEmbedding |
| 7 | +import pprint |
| 8 | +import requests |
| 9 | + |
| 10 | + |
| 11 | +class NoArgsException(Exception): |
| 12 | + pass |
| 13 | + |
| 14 | + |
| 15 | +class NotFoundException(Exception): |
| 16 | + pass |
| 17 | + |
| 18 | + |
| 19 | +# query reearth cms via public api |
| 20 | +def public_query( |
| 21 | + endpoint: str = None, prefecture: str = None, city_name: str = None |
| 22 | +) -> str: |
| 23 | + """Re:EarthのパブリックAPIを利用して都道府県、市町村名からCityGMLを取得する |
| 24 | +
|
| 25 | + Parameters |
| 26 | + ---------- |
| 27 | + endpoint : str |
| 28 | + APIのエンドポイント |
| 29 | + prefecture : str |
| 30 | + 都道府県 |
| 31 | + city_name : str |
| 32 | + 市町村 |
| 33 | +
|
| 34 | + Returns |
| 35 | + ------- |
| 36 | + str |
| 37 | + CityGMLのパス |
| 38 | + """ |
| 39 | + if endpoint == None or prefecture == None or city_name == None: |
| 40 | + raise NoArgsException |
| 41 | + res = None |
| 42 | + page = 1 |
| 43 | + hasMore = True |
| 44 | + while hasMore: |
| 45 | + response = requests.get(endpoint, params={"page": page}) |
| 46 | + if response.status_code == 200: |
| 47 | + obj = response.json() |
| 48 | + for item in obj["results"]: |
| 49 | + keys = item.keys() |
| 50 | + if "city_name" in keys and "prefecture" in keys: |
| 51 | + if ( |
| 52 | + item["city_name"] == city_name |
| 53 | + and item["prefecture"] == prefecture |
| 54 | + ): |
| 55 | + res = item["citygml"]["url"] |
| 56 | + return res |
| 57 | + hasMore = obj["hasMore"] |
| 58 | + print(hasMore) |
| 59 | + if hasMore: |
| 60 | + page += 1 |
| 61 | + else: |
| 62 | + hasMore = False |
| 63 | + if res == None: |
| 64 | + raise NotFoundException |
| 65 | + |
| 66 | + |
| 67 | +# check prefecture and city from given private api result |
| 68 | +def _check_prefecture_city(items, prefecture, city): |
| 69 | + # search each items from given prefecture and city |
| 70 | + for item in items: |
| 71 | + pref_check = False |
| 72 | + city_check = False |
| 73 | + for keys in item["fields"]: |
| 74 | + # Since the items are an array, whether each item have certain column varies. |
| 75 | + # This makes typing difficult. |
| 76 | + # So the fields are stored in the form of an array of key-value. |
| 77 | + if keys["key"] == "prefecture": |
| 78 | + if keys["value"] == prefecture: |
| 79 | + pref_check = True |
| 80 | + elif keys["key"] == "city_name": |
| 81 | + if keys["value"] == city: |
| 82 | + city_check = True |
| 83 | + elif keys["key"] == "citygml": |
| 84 | + try: |
| 85 | + # Asset is one. So it is not stored in the way of key-value and access directry. |
| 86 | + # But if the key does not exist, it raises an error |
| 87 | + url = keys["value"]["url"] |
| 88 | + except Exception as e: |
| 89 | + url = "" |
| 90 | + if pref_check and city_check: |
| 91 | + return url |
| 92 | + return None |
| 93 | + |
| 94 | + |
| 95 | +# query reearth cms via private api |
| 96 | +def private_query( |
| 97 | + endpoint: str = None, |
| 98 | + access_token: str = None, |
| 99 | + project: str = None, |
| 100 | + model: str = None, |
| 101 | + prefecture: str = None, |
| 102 | + city_name: str = None, |
| 103 | +): |
| 104 | + """Re:EarthのプライベートAPIを利用して都道府県、市町村名からCityGMLを取得する |
| 105 | +
|
| 106 | + Parameters |
| 107 | + ---------- |
| 108 | + endpoint : str |
| 109 | + APIのエンドポイント |
| 110 | + access_token : str |
| 111 | + APIのアクセストークン |
| 112 | + project : str |
| 113 | + プロジェクトのIDもしくはエイリアス |
| 114 | + model : str |
| 115 | + モデルのIDもしくはエイリアス |
| 116 | + prefecture : str |
| 117 | + 都道府県 |
| 118 | + city_name : str |
| 119 | + 市町村 |
| 120 | +
|
| 121 | + Returns |
| 122 | + ------- |
| 123 | + str |
| 124 | + CityGMLのパス |
| 125 | + """ |
| 126 | + configuration = reearthcmsapi.Configuration( |
| 127 | + host=endpoint, access_token=access_token |
| 128 | + ) |
| 129 | + # Enter a context with an instance of the API client |
| 130 | + with reearthcmsapi.ApiClient(configuration) as api_client: |
| 131 | + # Create an instance of the API class |
| 132 | + api_instance = items_project_api.ItemsProjectApi(api_client) |
| 133 | + |
| 134 | + # example passing only optional values |
| 135 | + path_params = { |
| 136 | + "projectIdOrAlias": project, |
| 137 | + "modelIdOrKey": model, |
| 138 | + } |
| 139 | + page = 1 |
| 140 | + perPage = 50 |
| 141 | + totalCount = 0 |
| 142 | + query_params = { |
| 143 | + "sort": "createdAt", |
| 144 | + "dir": "desc", |
| 145 | + "page": page, |
| 146 | + "perPage": perPage, |
| 147 | + "ref": "latest", |
| 148 | + "asset": AssetEmbedding("all"), |
| 149 | + } |
| 150 | + try: |
| 151 | + # Returns a list of items. |
| 152 | + api_response = api_instance.item_filter_with_project( |
| 153 | + path_params=path_params, |
| 154 | + query_params=query_params, |
| 155 | + ) |
| 156 | + items = api_response.body["items"] |
| 157 | + url = _check_prefecture_city(items, prefecture, city_name) |
| 158 | + if url != None: |
| 159 | + return url |
| 160 | + totalCount = api_response.body["totalCount"] |
| 161 | + except reearthcmsapi.ApiException as e: |
| 162 | + print("Exception when calling ItemsApi->item_filter: %s\n" % e) |
| 163 | + while (page * perPage) < totalCount: |
| 164 | + page += 1 |
| 165 | + query_params = { |
| 166 | + "sort": "createdAt", |
| 167 | + "dir": "desc", |
| 168 | + "page": page, |
| 169 | + "perPage": perPage, |
| 170 | + "ref": "latest", |
| 171 | + "asset": AssetEmbedding("all"), |
| 172 | + } |
| 173 | + try: |
| 174 | + # Returns a list of items. |
| 175 | + api_response = api_instance.item_filter_with_project( |
| 176 | + path_params=path_params, |
| 177 | + query_params=query_params, |
| 178 | + ) |
| 179 | + items = api_response.body["items"] |
| 180 | + url = _check_prefecture_city(items, prefecture, city_name) |
| 181 | + if url != None: |
| 182 | + return url |
| 183 | + except reearthcmsapi.ApiException as e: |
| 184 | + print("Exception when calling ItemsApi->item_filter: %s\n" % e) |
0 commit comments