Skip to content

Commit

Permalink
Merge pull request #25 from opendatasoft/update_v2.1_explore_and_meta…
Browse files Browse the repository at this point in the history
…data_rework_in_QGIS_plugin

Update API call URL and change metadata access after its rework
  • Loading branch information
5k4nd authored Jul 26, 2023
2 parents f90a9a3 + d98e53f commit fd6911b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
4 changes: 3 additions & 1 deletion Opendatasoft/metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name=Opendatasoft
description=Download datasets from Opendatasoft-powered data catalogs
about=This plugin allows one to directly import, in the GeoJSON format, any dataset from a private or public Opendatasoft portal. It uses the web Explore API v2 to fetch the data, thus you'll find this plugin in the web menu.
version=1.0.0
version=1.1.0
qgisMinimumVersion=3.0
author=Venceslas Roullier (Opendatasoft)
[email protected]
Expand All @@ -13,5 +13,7 @@ icon=icon.png

changelog=1.0.0
- Initial release: working plugin for listing datasets of an Opendatasoft catalog and fetching a specific one locally
changelog=1.1.0
- Fixes data fetching following Opendatasoft's 2.1 API breaking changes

tags=Opendatasoft,ods,open data,datasets
6 changes: 3 additions & 3 deletions Opendatasoft/ui_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ def updateSchemaTable(self):
else:
metadata = utils.import_dataset_metadata(self.domain(), self.dataset_id(), None)
first_record = utils.import_first_record(self.domain(), self.dataset_id(), None)
self.datasetNameLabel.setText("Dataset name: {}".format(metadata['results'][0]['default']['title']))
self.publisherLabel.setText("Publisher: {}".format(metadata['results'][0]['default']['publisher']))
self.datasetNameLabel.setText("Dataset name: {}".format(metadata['results'][0]['metas']['default']['title']))
self.publisherLabel.setText("Publisher: {}".format(metadata['results'][0]['metas']['default']['publisher']))
self.recordsNumberLabel.setText("Number of records: {}".format(
metadata['results'][0]['default']['records_count']))
metadata['results'][0]['metas']['default']['records_count']))
for field in metadata['results'][0]['fields']:
column_position = self.schemaTableWidget.columnCount()
self.schemaTableWidget.insertColumn(column_position)
Expand Down
13 changes: 7 additions & 6 deletions Opendatasoft/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def import_dataset_list(domain_url, apikey, include_non_geo_dataset, text_search
params['where'] = ["features='geo'"]

try:
first_query = requests.get("https://{}/api/v2/catalog/query".format(domain_url),
first_query = requests.get("https://{}/api/explore/v2.1/catalog/datasets".format(domain_url),
params=params,
headers=headers)
if first_query.status_code >= 500:
Expand All @@ -53,11 +53,12 @@ def import_dataset_list(domain_url, apikey, include_non_geo_dataset, text_search
except (requests.exceptions.ConnectionError, requests.exceptions.InvalidURL):
raise DomainError
json_dataset = first_query.json()
print(json_dataset)
total_count = json_dataset['total_count']
params['offset'] = V2_API_CHUNK_SIZE
query_size_limit = V2_QUERY_SIZE_LIMIT_DATA_OPENDATASOFT if domain_url == 'data.opendatasoft.com' else V2_QUERY_SIZE_LIMIT - V2_API_CHUNK_SIZE
while params['offset'] <= total_count and params['offset'] < query_size_limit:
query = requests.get("https://{}/api/v2/catalog/query".format(domain_url),
query = requests.get("https://{}/api/explore/v2.1/catalog/datasets".format(domain_url),
params=params,
headers=headers)
if query.status_code >= 500:
Expand All @@ -79,7 +80,7 @@ def import_dataset_metadata(domain_url, dataset_id, apikey):
if apikey:
headers['Authorization'] = 'apikey {}'.format(apikey)
try:
query = requests.get("https://{}/api/v2/catalog/query".format(domain_url),
query = requests.get("https://{}/api/explore/v2.1/catalog/datasets".format(domain_url),
params=params,
headers=headers)
except requests.exceptions.ConnectionError:
Expand All @@ -96,7 +97,7 @@ def import_first_record(domain_url, dataset_id, apikey):
if apikey:
headers['Authorization'] = 'apikey {}'.format(apikey)
try:
query = requests.get("https://{}/api/v2/catalog/datasets/{}/query".format(domain_url, dataset_id),
query = requests.get("https://{}/api/explore/v2.1/catalog/datasets/{}/records".format(domain_url, dataset_id),
params=params,
headers=headers)
except requests.exceptions.ConnectionError:
Expand Down Expand Up @@ -152,7 +153,7 @@ def import_dataset_to_qgis(domain, dataset_id, params):
params_no_limit = dict(params)
if 'limit' in params_no_limit.keys():
params_no_limit.pop('limit')
test_query = requests.get("https://{}/api/v2/catalog/datasets/{}/query".format(domain, dataset_id),
test_query = requests.get("https://{}/api/explore/v2.1/catalog/datasets/{}/records".format(domain, dataset_id),
params_no_limit)

except (requests.exceptions.ConnectionError, requests.exceptions.InvalidURL):
Expand All @@ -173,7 +174,7 @@ def import_dataset_to_qgis(domain, dataset_id, params):
if limit < -1:
raise NumberOfLinesError

imported_dataset = requests.get("https://{}/api/v2/catalog/datasets/{}/exports/geojson".format(domain, dataset_id),
imported_dataset = requests.get("https://{}/api/explore/v2.1/catalog/datasets/{}/exports/geojson".format(domain, dataset_id),
params, stream=True)
return imported_dataset

Expand Down

0 comments on commit fd6911b

Please sign in to comment.