Skip to content

Commit 96b09ef

Browse files
authored
Merge pull request #32 from eukarya-inc/feature/29/fetch_asset_url
(refs #29) added fetch citygml url
2 parents 35a92f7 + d4a62bb commit 96b09ef

File tree

13 files changed

+15017
-4
lines changed

13 files changed

+15017
-4
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ jobs:
1919
- name: Install dependencies
2020
run: |
2121
python -m pip install --upgrade pip
22-
pip install black pytest pytest-cov
22+
pip install black pytest pytest-cov httpretty
2323
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
2424
- name: Test with pytest
2525
run: |
26-
pytest --cov=plateauutils --cov-fail-under=90
26+
pytest --cov=plateauutils --cov-fail-under=90

dev-requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Cython==3.0.2
1111
docutils==0.20.1
1212
earcut==1.1.5
1313
exceptiongroup==1.1.1
14+
frozendict==2.3.8
15+
httpretty==1.1.4
1416
idna==3.4
1517
imagesize==1.4.1
1618
importlib-metadata==6.6.0
@@ -48,6 +50,7 @@ python-dateutil==2.8.2
4850
pytz==2023.3.post1
4951
pyzmq==25.1.1
5052
readme-renderer==40.0
53+
reearthcmsapi==0.0.3
5154
requests==2.31.0
5255
requests-toolbelt==1.0.0
5356
rfc3986==2.0.0

doc/plateauutils.citygmlfinder.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
plateauutils.citygmlfinder パッケージ
2+
======================================
3+
4+
plateauutils.citygmlfinder.from_reearth_cms モジュール
5+
--------------------------------------------------
6+
7+
.. automodule:: plateauutils.citygmlfinder.from_reearth_cms
8+
:members:
9+
:undoc-members:
10+
:show-inheritance:
11+

doc/plateauutils.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ plateauutils パッケージ
66
plateauutils.mesh_geocorder
77
plateauutils.tile_list
88
plateauutils.parser
9-
plateauutils.flood_converter
9+
plateauutils.citygmlfinder
10+
plateauutils.flood_converter

plateauutils/citygmlfinder/__init__.py

Whitespace-only changes.
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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

Comments
 (0)