Usage¶
Load API credentials from a JSON or INI file¶
Isogeo delivers API credentials in a JSON file. Its structure depends on the kind of oAuth2 application you are developing. Please referer to the API documentation to know more about different types of oAuth2 application.
For example, here is the JSON structure for a “workgroup” application:
{
"web": {
"client_id": "python-minimalist-sdk-test-uuid-1a2b3c4d5e6f7g8h9i0j11k12l",
"client_secret": "application-secret-1a2b3c4d5e6f7g8h9i0j11k12l13m14n15o16p17Q18rS",
"auth_uri": "https://id.api.isogeo.com/oauth/authorize",
"token_uri": "https://id.api.isogeo.com/oauth/token"
}
}
The module isogeo_pysdk.utils comes with a method to load automatically credentials from JSON and INI files:
# load package
from isogeo_pysdk import Isogeo, IsogeoUtils
# instanciate IsogeoUtils as utils
utils = IsogeoUtils()
# load from file
api_credentials = utils.credentials_loader("client_secrets_group.json")
# could also be:
# api_credentials = utils.credentials_loader("client_secrets_user.json")
# api_credentials = utils.credentials_loader("client_secrets.ini")
# authenticate your client application
isogeo = Isogeo(client_id=api_credentials.get("client_id"),
client_secret=api_credentials.get("client_secret")
)
# get the token
isogeo.connect()
Keys of returned dict:
- auth_mode
- client_id
- client_secret
- scopes
- uri_auth
- uri_base
- uri_redirect
- uri_token
URL Builder for web applications¶
- 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:
from isogeo_pysdk import IsogeoUtils
utils = IsogeoUtils()
url = utils.get_edit_url(md_id="0269803d50c446b09f5060ef7fe3e22b",
md_type="vector-dataset",
owner_id="32f7e95ec4e94ca3bc1afda960003882",
tab="attributes")
Get OpenCatalog URL for a metadata¶
from isogeo_pysdk import IsogeoUtils
utils = IsogeoUtils()
oc_url = utils.get_view_url(webapp="oc",
md_id="0269803d50c446b09f5060ef7fe3e22b",
share_id="1e07910d365449b59b6596a9b428ecd9",
share_token="TokenOhDearToken")
Get CSW GetRecordById for a metadata¶
from isogeo_pysdk import IsogeoUtils
utils = IsogeoUtils()
uuid_md_source = "82e73458e29a4edbaf8bfce9e16fa78b"
csw_getrecord_url = utils.get_view_url(webapp="csw_getrec",
md_uuid_urn=utils.convert_uuid(uuid_md_source, 2),
share_id="ShareUniqueIdentifier",
share_token="TokenOhDearToken")
Register a custom webapp and get URL¶
from isogeo_pysdk import IsogeoUtils
utils = IsogeoUtils()
# 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 = utils.get_view_url(md_id="0269803d50c446b09f5060ef7fe3e22b",
webapp="PPIGE v3")
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
# authenticate your client application
isogeo = Isogeo(client_id=app_id,
client_secret=app_secret)
# 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.get("results"):
title = md.get('title')
xml_stream = isogeo.xml19139(
md.get("_id")
)
with open("{}.xml".format(title), 'wb') as fd:
for block in xml_stream.iter_content(1024):
fd.write(block)
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)
# 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.get("results"):
for link in filter(lambda x: x.get("type") == "hosted", md.get("links")):
dl_stream = isogeo.dl_hosted(
resource_link=link)
filename = re.sub(r'[\\/*?:"<>|]', "", dl_stream[1])
with open(filename, 'wb') as fd:
for block in dl_stream[0].iter_content(1024):
fd.write(block)
Example: