Generate Isochrones
Learn how to create isochrones with Kinetica's Graph
Learn how to create isochrones with Kinetica's Graph
The following is a complete example, using the Python API, of generating images containing isochrones via the /visualize/isochrone and /wms endpoints using an existing graph.
The prerequisites for running the isochrones example are listed below:
The native Kinetica Python API is accessible through the following means:
The Python package manager, pip, is required to install the API from PyPI.
Install the API:
|
|
Test the installation:
|
|
If Import Successful is displayed, the API has been installed as is ready for use.
In the desired directory, run the following, but be sure to replace <kinetica-version> with the name of the installed Kinetica version, e.g., v7.1:
|
|
Change directory into the newly downloaded repository:
|
|
In the root directory of the unzipped repository, install the Kinetica API:
|
|
Test the installation (Python 2.7 (or greater) is necessary for running the API example):
|
|
The example script references the dc_shape.csv
data file,
mentioned in the Prerequisites, in the current local directory, by default.
This directory can specified as a parameter when running the script.
This example is going to demonstrate the following scenarios using isochrones either via the /visualize/isochrone endpoint or the /wms endpoint:
This example will use two connections, one to the database and one to the WMS endpoint, configured as follows:
|
|
|
|
Many constants are defined at the beginning of the script:
|
|
|
|
|
|
|
|
One graph is used for the isochrones examples utilized in the script: dc_shape_graph, a graph based on the dc_shape dataset (the CSV file mentioned in Prerequisites).
Note
The example script will first check to see if the dc_shape table and dc_shape_graph exist; if either does not, they will be created.
The dc_shape_graph is created with the following characteristics:
|
|
The graph output to WMS:
First, additional contour and isochrones options are provided:
Then, the first group of isochrones is calculated using the following parameters and an image is generated using /visualize/isochrone endpoint:
|
|
The image is pulled from the response and written locally:
|
|
The generated image for IAD isochrones:
The second group of isochrones is calculated using the following parameters:
Note
The solve_direction is still to_source at this point, so the second group of isochrones is calculated for leading to DCA
|
|
The image is pulled from the response and written locally:
|
|
The generated image for DCA isochrones:
Before the last example, the solve direction is updated to reflect that the isochrones are calculated leading from Kinetica HQ:
|
|
Finally, the third group of isochrones can be calculated using the following parameters:
|
|
The image is pulled from the response and written locally:
|
|
The generated image for Kinetica HQ (leading from) isochrones:
To find the shared area between the three contours that were just created, the ST_INTERSECTION() geospatial function is used to join the three tables together:
|
|
The shared area geometry output to WMS:
The three isochrones overlaid on top of each other:
To visualize isochrones via the /wms endpoint, the WMS payload must be constructed. Consult /wms for more information on WMS parameters, styles, and style options.
WMS payloads require a small set of standard parameters that will generally be the same across all WMS requests:
|
|
The next settings inform WMS to generate isochrones (and to make Isochrones parameters available for use) and to set the image width to 512 pixels. The image height is set to -1, which the database will replace with the value resulting from multiplying the aspect ratio by the image width.
|
|
The following parameters define how the isochrones will be calculated. Notice these are similar to the previous examples:
|
|
Isochrone style options are passed in using the constants:
|
|
Isochrone contour options are passed in. Additional contour style parameters are passed in for additional customization over the generated isochrones:
|
|
Lastly, isochrone options are passed in. Note the change in direction for the solve_direction as we want isochrones leading to Kinetica HQ in this instance.
|
|
The WMS request is sent and the image is pulled from the response and written locally:
|
|
|
|
The generated image for Kinetica HQ (leading to) isochrones:
With labels and map data underneath:
Tip
Using the solve_table (TABLE_K_ISO_SOLVE), you can class break on the z (cost) column in WMS to see individual points and their relative cost:
Included below is a complete example containing all the above requests, the data files, and output.
To run the complete sample, ensure that:
isochrones.py
script is in the current directorydc_shape.csv
file is in the current directory or use the
data_dir parameter to specify the local directory containing itThen, run the following:
|
|