pip install rasdapy
You can download it from http://tutorial.rasdaman.org/rasdapy-tutorial/rasdapy.ipynb and run. (after you installed rasdaman, rasdapy and Jupyter Notebook).
After rasdapy installation, you can import rasdapy to your Python script like others libraries.
from rasdapy.db_connector import DBConnector
from rasdapy.query_executor import QueryExecutor
The DBConnector maintains the connection to rasdaman. In order to connect it is necessary to specify the host (default: localhost) and port (default: 7001) on which rasmgr is running, as well as valid rasdaman username with read and write permissions (default: rasadmin) and password (default: rasadmin).
db_connector = DBConnector("localhost", 7001, "rasadmin", "rasadmin")
db_connector.open()
QueryExcutor is the interface through which rasql queries (create, insert, update, delete, etc...) are executed.
query_executor = QueryExecutor(db_connector)
It is a good practice to check the connection to rasdaman by this Rasql query and have an idea about which collections can be queries before-hand. Here, only 1 collection was created in rasdaman, named rgb.
In rasdaman databases, arrays are grouped into collections. All elements of a collection share the same array type definition. Collections form the basis for array handling, just as tables do in relational databasetechnology.
Note: We use query_executor.execute_read() because SELECT does not need the write permission in transaction to rasdaman.
collection_list = query_executor.execute_read("select c from RAS_COLLECTIONNAMES as c")
print(collection_list)
We want to create a new rasdaman collection using rasdapy in this demonstration which can be used to query later. The collection is supposed to store a 2D PNG image with dataType: char.
Note: We use query_executor.execute_write() because CREATE needs the write permission in transaction to rasdaman.
query_executor.execute_write("create collection test_mr GreySet")
The collection is empty and we want to import data from a 2D PNG file as multi-dimensional array (MDD). You can download the input file from: http://rasdaman.org/browser/systemtest/testcases_mandatory/test_select/testdata/mr_1.png and save it to your local system which rasdaman can have permission to read. In this tutorial, we save the file to: /home/rasdaman/mr.png.
Note: We use query_executor.execute_update_from_file() because INSERT needs the write permission in transaction to rasdaman and it is used to INSERT/UPDATE rasdaman collections.
query_executor.execute_update_from_file("insert into test_mr values decode($1)", "/home/rasdaman/mr.png")
When the INSERT query is successful, the MDD is created inside the collection test_mr. We can see the spatial domain (width x height) via this SELECT query:
sdom = query_executor.execute_read("select sdom(c) from test_mr as c")
print(sdom)
We want to get the average of all pixel values from test collection.
result = query_executor.execute_read("select (char)avg_cells(c) from test_mr as c")
print(result)
Select a particular subset of array in collection test_mr. This query will return raw array data that can be converted to a Numpy ndarray. Then, you can do all the features of Numpy ndarray normally.
result = query_executor.execute_read("select m[30:40 , 20:40] from test_mr as m")
numpy_array = result.to_array()
print(numpy_array.shape)
print(numpy_array)
Using matplotlib library, you can display the raw data from rasdaman as Numpy ndarray easily.
import matplotlib.pyplot as plt
import numpy as np
result = query_executor.execute_read("select m from test_mr as m")
numpy_array = result.to_array()
# Plot the grid
plt.imshow(numpy_array)
plt.show()
rasdaman supports multilple type of encodes (e.g: jpeg, png, tiff, csv, json,...) so you can select data from rasdaman with encoded format and write the result to a file normally (e.g: /tmp/output.png).
result = query_executor.execute_read('select encode(m[30:40 , 20:40], "png") from test_mr as m')
with open("/tmp/output.png", "wb") as binary_file:
binary_file.write(result.data)
It is important to close the connection to rasdaman when you've finished your Python script. That will release the connection to rasserver and allow another client can connect to this server afterwards.
db_connector.close()
from rasdapy.db_connector import DBConnector
from rasdapy.query_executor import QueryExecutor
db_connector = DBConnector("localhost", 7001, "rasadmin", "rasadmin")
query_executor = QueryExecutor(db_connector)
db_connector.open()
try:
query_executor.execute_read("...")
query_executor.execute_write("...")
# ... more Python code
finally:
db_connector.close()