Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(refs #29) added fetch citygml url #32

Merged
merged 14 commits into from
Sep 29, 2023
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install black pytest pytest-cov
pip install black pytest pytest-cov httpretty
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Test with pytest
run: |
pytest --cov=plateauutils --cov-fail-under=90
pytest --cov=plateauutils --cov-fail-under=90
3 changes: 3 additions & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Cython==3.0.2
docutils==0.20.1
earcut==1.1.5
exceptiongroup==1.1.1
frozendict==2.3.8
httpretty==1.1.4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing reearthcmsapi==0.0.2 for dev-requirements.txt

idna==3.4
imagesize==1.4.1
importlib-metadata==6.6.0
Expand Down Expand Up @@ -48,6 +50,7 @@ python-dateutil==2.8.2
pytz==2023.3.post1
pyzmq==25.1.1
readme-renderer==40.0
reearthcmsapi==0.0.3
requests==2.31.0
requests-toolbelt==1.0.0
rfc3986==2.0.0
Expand Down
11 changes: 11 additions & 0 deletions doc/plateauutils.citygmlfinder.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
plateauutils.citygmlfinder パッケージ
======================================

plateauutils.citygmlfinder.from_reearth_cms モジュール
--------------------------------------------------

.. automodule:: plateauutils.citygmlfinder.from_reearth_cms
:members:
:undoc-members:
:show-inheritance:

3 changes: 2 additions & 1 deletion doc/plateauutils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ plateauutils パッケージ
plateauutils.mesh_geocorder
plateauutils.tile_list
plateauutils.parser
plateauutils.flood_converter
plateauutils.citygmlfinder
plateauutils.flood_converter
Empty file.
184 changes: 184 additions & 0 deletions plateauutils/citygmlfinder/from_reearth_cms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
# coding: utf-8
import reearthcmsapi
from reearthcmsapi.apis.tags import items_project_api
from reearthcmsapi.model.model import Model
from reearthcmsapi.model.versioned_item import VersionedItem
from reearthcmsapi.model.asset_embedding import AssetEmbedding
import pprint
import requests


class NoArgsException(Exception):
pass


class NotFoundException(Exception):
pass


# query reearth cms via public api
def public_query(
endpoint: str = None, prefecture: str = None, city_name: str = None
) -> str:
"""Re:EarthのパブリックAPIを利用して都道府県、市町村名からCityGMLを取得する

Parameters
----------
endpoint : str
APIのエンドポイント
prefecture : str
都道府県
city_name : str
市町村

Returns
-------
str
CityGMLのパス
"""
if endpoint == None or prefecture == None or city_name == None:
raise NoArgsException
res = None
page = 1
hasMore = True
while hasMore:
response = requests.get(endpoint, params={"page": page})
if response.status_code == 200:
obj = response.json()
for item in obj["results"]:
keys = item.keys()
if "city_name" in keys and "prefecture" in keys:
if (
item["city_name"] == city_name
and item["prefecture"] == prefecture
):
res = item["citygml"]["url"]
return res
hasMore = obj["hasMore"]
print(hasMore)
if hasMore:
page += 1
else:
hasMore = False
if res == None:
raise NotFoundException


# check prefecture and city from given private api result
def _check_prefecture_city(items, prefecture, city):
# search each items from given prefecture and city
for item in items:
pref_check = False
city_check = False
for keys in item["fields"]:
# Since the items are an array, whether each item have certain column varies.
# This makes typing difficult.
# So the fields are stored in the form of an array of key-value.
if keys["key"] == "prefecture":
if keys["value"] == prefecture:
pref_check = True
elif keys["key"] == "city_name":
if keys["value"] == city:
city_check = True
elif keys["key"] == "citygml":
try:
# Asset is one. So it is not stored in the way of key-value and access directry.
# But if the key does not exist, it raises an error
url = keys["value"]["url"]
except Exception as e:
url = ""
if pref_check and city_check:
return url
return None


# query reearth cms via private api
def private_query(
endpoint: str = None,
access_token: str = None,
project: str = None,
model: str = None,
prefecture: str = None,
city_name: str = None,
):
"""Re:EarthのプライベートAPIを利用して都道府県、市町村名からCityGMLを取得する

Parameters
----------
endpoint : str
APIのエンドポイント
access_token : str
APIのアクセストークン
project : str
プロジェクトのIDもしくはエイリアス
model : str
モデルのIDもしくはエイリアス
prefecture : str
都道府県
city_name : str
市町村

Returns
-------
str
CityGMLのパス
"""
configuration = reearthcmsapi.Configuration(
host=endpoint, access_token=access_token
)
# Enter a context with an instance of the API client
with reearthcmsapi.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = items_project_api.ItemsProjectApi(api_client)

# example passing only optional values
path_params = {
"projectIdOrAlias": project,
"modelIdOrKey": model,
}
page = 1
perPage = 50
totalCount = 0
query_params = {
"sort": "createdAt",
"dir": "desc",
"page": page,
"perPage": perPage,
"ref": "latest",
"asset": AssetEmbedding("all"),
}
try:
# Returns a list of items.
api_response = api_instance.item_filter_with_project(
path_params=path_params,
query_params=query_params,
)
items = api_response.body["items"]
url = _check_prefecture_city(items, prefecture, city_name)
if url != None:
return url
totalCount = api_response.body["totalCount"]
except reearthcmsapi.ApiException as e:
print("Exception when calling ItemsApi->item_filter: %s\n" % e)
while (page * perPage) < totalCount:
page += 1
query_params = {
"sort": "createdAt",
"dir": "desc",
"page": page,
"perPage": perPage,
"ref": "latest",
"asset": AssetEmbedding("all"),
}
try:
# Returns a list of items.
api_response = api_instance.item_filter_with_project(
path_params=path_params,
query_params=query_params,
)
items = api_response.body["items"]
url = _check_prefecture_city(items, prefecture, city_name)
if url != None:
return url
except reearthcmsapi.ApiException as e:
print("Exception when calling ItemsApi->item_filter: %s\n" % e)
Loading