Usage¶
URL Builder for web applications¶
All examples in this section must be precede by the following code:
from isogeo_pysdk import IsogeoUtils, Isogeo
utils = IsogeoUtils()
# authenticate your client application
isogeo = Isogeo(
client_id=app_id,
client_secret=app_secret,
auto_refresh_url=isogeo_token_uri
)
# get the token
isogeo.connect()
Isogeo metadata can be displyed in others web applications. Some webapps are built-in:
- OpenCatalog (oc)
- *Data portal by PixUp (pixup_portal)
- *CSW GetCapabilities (for a share)
- *CSW GetRecords (for a metadata)
It’s also possible to register a custom web app (see below).
Get URL to online editor for a metadata¶
A metadata can only be edited by an authenticated Isogeo user (with editor level at least). A built-in method make it easy to contruct it:
md = isogeo.metadata.get(md_id="36fde4261bcb4ef2a849d94a50488713")
url = utils.get_edit_url(metadata=md, tab="attributes")
Get OpenCatalog URL for a metadata¶
oc_args = {
"md_id": "36fde4261bcb4ef2a849d94a50488713",
"share_id": "344d51c3edfb435daf9d98d948fa207e",
"share_token": "TokenOhDearToken"
}
oc_url = utils.get_view_url(webapp="oc", **oc_args)
Get CSW GetRecordById for a metadata¶
uuid_md_source = "36fde4261bcb4ef2a849d94a50488713"
csw_getrec_args = {
"md_uuid_urn": utils.convert_uuid(uuid_md_source, 2),
"share_id": "344d51c3edfb435daf9d98d948fa207e",
"share_token": "TokenOhDearToken"
}
csw_getrec_url = utils.get_view_url(webapp="csw_getrec", **csw_getrec_args)
Register a custom webapp and get URL¶
# register the web app
utils.register_webapp(
webapp_name="PPIGE v3",
webapp_args=["md_id", ],
webapp_url="https://www.ppige-npdc.fr/portail/geocatalogue?uuid={md_id}"
)
# get url
custom_url_args = {
"md_id": "36fde4261bcb4ef2a849d94a50488713",
"share_id": "344d51c3edfb435daf9d98d948fa207e",
"share_token": "TokenOhDearToken"
}
custom_url = utils.get_view_url(webapp="PPIGE v3", **custom_url_args)
Download metadata as XML ISO 19139¶
In Isogeo, every metadata resource can be downloaded in its XML version (ISO 19139 compliant). The Python SDK package inclue a shortcut method:
from isogeo_pysdk import Isogeo, Metadata
# authenticate your client application
isogeo = Isogeo(
client_id=app_id,
client_secret=app_secret,
auto_refresh_url=isogeo_token_uri
)
# get the token
isogeo.connect()
# search metadata
search_to_be_exported = isogeo.search(
page_size=10,
query="type:dataset",
whole_results=0
)
# loop on results and export
for md in search_to_be_exported.results:
metadata = Metadata.clean_attributes(md)
title = metadata.title
xml_stream = isogeo.metadata.download_xml(metadata)
with open("{}.xml".format(title), 'wb') as fd:
for block in xml_stream.iter_content(1024):
fd.write(block)
# properly closing connection
isogeo.close()
Others examples:
Download hosted data from Isogeo cloud¶
Administrators and editors can link raw data and docs (.zip, .pdf…) to metadata to allow final users to access the data. To do that, it’s possible to upload data to Isogeo cloud (Azure blob storage).Through the API, it’s possible to download these data:
from isogeo_pysdk import Isogeo
# authenticate your client application
isogeo = Isogeo(
client_id=app_id,
client_secret=app_secret,
auto_refresh_url=isogeo_token_uri
)
# get the token
isogeo.connect()
# search with _include = links and action = download
latest_data_modified = isogeo.search(
page_size=10,
order_by="modified",
whole_results=0,
query="action:download",
include=("links",),
)
# parse links and download hosted data recursively
for md in latest_data_modified.results:
for link in filter(lambda x: x.get("type") == "hosted", md.get("links")):
dl_stream = isogeo.metadata.links.download_hosted(link=link)
filename = re.sub(r'[\\/*?:"<>|]', "", dl_stream[1])
with open(dl_stream[1], "wb") as fd:
for block in dl_stream[0].iter_content(1024):
fd.write(block)
# properly closing connection
isogeo.close()
Example:
Add versions to a format¶
It is necessary to update Isogeo database with new formats versions, so that users can properly fill metadata sheets on app.isogeo.com.
from isogeo_pysdk import Isogeo
# -- Authentication using 'user_legacy' flow
# Isogeo client
isogeo = Isogeo(
auth_mode="user_legacy",
client_id=api_dev_id,
client_secret=api_dev_secret,
auto_refresh_url=isogeo_token_uri
)
# getting a token
isogeo.connect(
username=isogeo_user_name,
password=isogeo_user_password,
)
# get a format
fmt_postgis = isogeo.formats.get("postgis")
# add versions to this format
fmt_postgis.versions.extend(li_new_versions)
# update the format with added versions
fmt_postgis_updated = isogeo.formats.update(fmt_postgis)
# properly closing connection
isogeo.close()