Quickstart Guide¶
About¶
Simplifies using the India Urban Data Exchange IUDX platform. Provides an API interface to perform scientific computing over various smart city resources. Vist the IUDX Catalogue to discover datasets of your interest.
IUDX Python SDK is developed entirely in Python3 and uses IUDX core apis to provide a simple interface to access smart city’s data.
Installation¶
IUDX Python SDK can be directly downloaded using the pip (Python Package Installer) using the following command:
pip install git+https://github.com/datakaveri/iudx-python-sdk
Sample Ipython Notebooks¶
Example Usage¶
Catalogue text search example¶
Get the catalogue list of all sensors based on a text search query.
from iudx.cat.Catalogue import Catalogue
from iudx.cat.CatalogueQuery import CatalogueQuery
# creating an object of Catalogue class using cat_url.
cat = Catalogue(
cat_url="https://api.catalogue.iudx.io/iudx/cat/v1",
headers={"content-type": "application/json"}
)
# creating a query for text search
cat_query = CatalogueQuery()
query = cat_query.text_search("aqm")
# fetching the search response for the query
result = cat.search_entity(query)
print(f"RESULTS: {result.documents}") # get all the search documents as json.
print(f"STATUS: {result.status}") # get the status for the search response.
print(f"TOTAL HITS: {result.total_hits}") # get the count of total results fetched.
Resources get latest example¶
Get the latest data for specific entity id.
from iudx.rs.ResourceServer import ResourceServer
from iudx.rs.ResourceQuery import ResourceQuery
# entity id for the pune env aqm sensor.
entity_id = "datakaveri.org/04a15c9960ffda227e9546f3f46e629e1fe4132b/rs.iudx.org.in/pune-env-aqm/f36b4669-628b-ad93-9970-f9d424afbf75"
# creating an object of ResourceServer class using rs_url.
rs = ResourceServer(
rs_url="https://rs.iudx.org.in/ngsi-ld/v1",
headers={"content-type": "application/json"}
)
# creating a query for fetching latest data for the entity_id.
rs_query = ResourceQuery()
rs_entity = rs_query.add_entity(entity_id)
# fetch results for a list of entities.
results = rs.get_latest([rs_entity])
# printing results
print(f"RESULTS: {results[0].results}") # get the result data of the resource query.
print(f"STATUS: {results[0].type}") # get the status code for the response.
Resources during example¶
Get the during data for specific entity id.
from iudx.rs.ResourceServer import ResourceServer
from iudx.rs.ResourceQuery import ResourceQuery
# entity id for the pune env aqm sensor.
entity_id = "datakaveri.org/04a15c9960ffda227e9546f3f46e629e1fe4132b/rs.iudx.org.in/pune-env-aqm/f36b4669-628b-ad93-9970-f9d424afbf75"
# creating an object of ResourceServer class using rs_url.
rs = ResourceServer(
rs_url="https://rs.iudx.org.in/ngsi-ld/v1",
headers={"content-type": "application/json"}
)
# creating a query for fetching latest data for the entity_id.
rs_query = ResourceQuery()
rs_entity = rs_query.add_entity(entity_id)
# create a during query for a time interval.
during_query = rs_entity.during_search(
start_time="2021-01-01T14:20:00Z",
end_time="2021-01-09T14:20:00Z"
)
# fetch results for the list of during queries.
results = rs.get_data([during_query])
# printing results
print(f"RESULTS: {results[0].results}") # get the result data of the resource query.
print(f"STATUS: {results[0].type}") # get the status code for the response.
Entity data download example¶
Get the data for the specific entity and download the generated pandas.dataframe as a .CSV zipped file.
from iudx.entity.Entity import Entity
# entity id for the pune env aqm sensor.
entity_id = "datakaveri.org/04a15c9960ffda227e9546f3f46e629e1fe4132b/rs.iudx.org.in/pune-env-aqm/f36b4669-628b-ad93-9970-f9d424afbf75"
# Creating an entity for the sensor.
entity = Entity(entity_id)
# create a during query for a time interval.
df = entity.during_search(
start_time="2021-01-01T14:20:00Z",
end_time="2021-01-09T14:20:00Z"
)
display(df.head())
# To download the dataset
file_name = "IUDX_data" # custom name of the file
file_type = "csv" # can be CSV or JSON
# download method for saving data to zip file.
entity.download(file_name=file_name)
CLI Usage¶
Download temporal data¶
Download the data based on a during entity query.
# Sample command
iudx
--entity <entity_id>
--token <token_id> (only for private resources which requires auth)
--start <start_timestamp>
--end <end_timestamp>
--download <file_name>
--type <file_type_csv_or_json>
# Example
iudx --entity datakaveri.org/04a15c9960ffda227e9546f3f46e629e1fe4132b/rs.iudx.org.in/pune-env-aqm/f36b4669-628b-ad93-9970-f9d424afbf75 --start 2021-01-01T14:20:00Z --end 2021-01-07T14:20:00Z --download test_file --type csv
Print latest data¶
Print the latest entity data in the console.
# Sample command
iudx
--entity <entity_id>
--token <token_id> (only for private resources which requires auth)
--latest
# Example
iudx --entity datakaveri.org/04a15c9960ffda227e9546f3f46e629e1fe4132b/rs.iudx.org.in/pune-env-aqm/f36b4669-628b-ad93-9970-f9d424afbf75 --latest
NOTE: --token is required for the entities which are not public.
Use iudx --help to know more about all possible options.