SAP HANA Geo-Spatial Features

SAP HANA Geospatial processing feature was launched with SAP HANA SP6.This enables organizations to store their Geospatial data along with Business data in the same instance of HANA. By extending the HANA Platform capabilities customers can perform operations on spatial data like calculating the distance between geometries, determining the union or intersection of multiple objects. These calculations are performed using predicates, such as intersects, contains, and crosses.

We can illustrate the concept based on a use case on parliamentary constituencies in INDIA.

map

Spatial Data:

Spatial data is data that describes the position, shape, and orientation of objects in a defined space. Spatial data is represented as 2D geometries in the form of points, line strings, and polygons.

SAP HANA has brought in some new spatial data types like POINT (ST_POINT) and Geometry (ST_GEOMETRY) to store spatial information. A point is like a fixed single location in space and will be represented by X and Y co-ordinates (*can also have a Z co-ordinate in case of 3D space).

A Geometry is like a super class container and can store the below type within it.

Spatial feature in SAP HANA:

HANA Spatial delivers the ability to store and process geospatial data types like ST_POINT, ST_GEOMETRY, etc.

These data types allow application developers to associate spatial information with their data. For example, a table representing companies could store the location of the company as a point, or store the delivery area for the company as a polygon.

SAP HANA also delivers Nokia mapping services as part of the HANA Spatial features and this can be used to create apps using HANA XS engine through which Nokia mapping services can be called using the Nokia API.

Once Spatial data is loaded into HANA you can create or extend HANA models (Analytic, Calculation Views) using HANA Studio and make these models available for analysis and visualization through SAP Lumira or custom HTML5 applications. SAP HANA SQL script has been extended to support the Spatial SQL MM Standards for storing and accessing geospatial data.

Spatial Type Hierarchy:

SAP HANA has brought in some new spatial data types like POINT (ST_POINT) and Geometry (ST_GEOMETRY) to store spatial information. A point is like a fixed single location in space and will be represented by X and Y co-ordinates (*can also have a Z co-ordinate in case of 3D space).
A Geometry is like a super class container and can store the below type within it. In Geometry data type we can store any of the child types like Line, Polygon, or Point.

The following diagram illustrates the hierarchy of the ST_Geometry data types:

diagram

Below you can see how we can store and retrieve data from a Point or Geometry Type

Point Geometry
SET SCHEMA “DEMO_SPA”;
create column table spatial_geom
(
shape ST_GEOMETRY
);
insert into spatial_geom values (new ST_POINT (0.0, 0.0) );
insert into spatial_geom values (new ST_POLYGON (‘POLYGON ((0.0 0.0, 4.0 0.0, 2.0 2.0, 0.0 0.0))’) );
select shape.ST_AsGeoJSON() from spatial_geom;
Output:-
SET SCHEMA “DEMO_SPA”;
create column table spatial point
(
point ST_POINT
);
insert into spatial point values (new ST_POINT (0.0, 0.0));
select point.ST_AsGeoJSON () from spatial point;
Output:-

So, basically in a Geometry data type we can store any of the child types like Line, Polygon, or Point. You can also see that we are querying the data as GeoJson which is a special JSON format for encoding a variety of geometrical data structures and it is easily understood by most of the MAP client APIs.

SAP HANA also provides a list of other means to extract data from geometrical data structures apart from GeoJson like Well Known Text (WKT), Well Known Binary (EKB), etc.

Now coming to the application where-in I am showing all the parliamentary constituencies of India.

So, first I had to collect the shape files for all the constituencies of INDIA. Finally, after some amount of research here and there I finally got the data of the Indian parliamentary constituency.

The data mostly often in ESRI shape and SAP HANA supported the shape file import into it.
So, in order to load shape file into HANA you need to perform the 4 simple steps stated below:-

1. Download Putty(http://www.putty.org/) and PSCP(http://www.nber.org/pscp.html)
2. Copy the shape file(zip it first) from your local machine into HANA using PSCP
C:\>pscp.exe@:
3. You can login to your server using putty and unzip the files.
4. You can import the shape files in HANA by running the below command.
   IMPORT “Schema_Name”.”Table_Name” AS SHAPEFILE FROM ‘path to shape file’

So, once I imported my shape files into HANA, the data looked like below:-

pgm3

So, as you can see I had the list of all the constituencies along with the following information:-

1. The state to which it belongs, the ruling party, area, and the color code(some of the fields were added later).
2. The shape information.

I can also see the shape as GeoJson and the data looked like as shown below.

pgm4

So, you can see that most of the shape are really complex and are created using polygons and multipolygons with a large number of points.

In this part, I will show how the data was exposed from HANA as GeoJson using XSJS and its consumption by Leaflet Map Client.

The application basically supports two features:-

1. It displays all the parliamentary constituencies and on hovering on each of the constituency, you can see more details about the constituency.

2. You can draw a polygon or a rectangle in the MAP around the constituencies and you will get the number of seats won by each party for constituencies which comes within the area of the polygon or the rectangle.

I have created 2 XSJS service for each of the above two features.

For the 1st feature, I am selecting the constituency information along with the shape information and returning the data in GeoJson format.
The ST_AsGeoJson() is a function provided by HANA Geospatial which can be used to return the data in GeoJson format. I am fetching the data in this format and adding it to a FeatureCollection as you can see below.

pgm5

The results returned by this service are shown below:-

pgm6

You can add as many features(like ST_NAME, color) you want under the feature collection but the shape will be stored under the geometry section of each feature as seen above.

The second XSJS service will be triggered whenever you draw a polygon on the MAP. I get the polygon co-ordinates using the MAP APIs and call the service by passing them as input. Inside the service I try to fetch all the constituencies which lies within the polygon by using the Geo-Spatial Function ST_Within() as shown below.

pgm7

The results returned from this service will look like as shown below.

pgm8

I am consuming the data returned from the XSJS service in my HTML5 application using AJAX. I used Leaflet.js APIs to plot and display the MAP.

It is done by following the below steps:-

Step 1 :- In Leaflet.js you need to first create a MAP instance and linked it to your map div.

var map = L.map(‘map’, {
zoomControl : true
}).setView([ 22.527636, 78.832675 ], 4);

Step 2:- You need to add the tile layer to the map. The tile can be of anything as leaflet is open source. It can be of Openstreetmap as well as Cloudmade.

Step 3:-  Then, you can do an AJAX call to get the constituencies data in the form of GeoJson and add it to the map.

function querystates(statesData){
$.ajax({
url: ‘../model/geodata.xsjs’,
data: statesData,
success: function (statesData) {
$(‘#wrapper’).hide();
geojson = L.geoJson(statesData, {
style : style,
onEachFeature : onEachFeature
}).addTo(map);
}
});
}

Step 4:-   On Each Feature which is nothing but a constituency you can perform further actions on mouseover, mouseout, etc.
function onEachFeature(feature, layer) {
layer.on({
mouseover : highlightFeature,
mouseout : resetHighlight,
click : zoomToFeature
});
}

And that`s it the application is up and running. For drawing shapes on MAP we need to use Leaflet Draw APIs which can be found in the link Leaflet/Leaflet.draw · GitHub.

You can also find the entire code for HTML 5 UI as well as the XSJS in the GITHUB link stated here:- https://github.com/trinoy/gisdemo.
where in you can see how I am using the Leaflet Draw APIs to create the shapes. The main file for drawing the MAP is map.js which is available under the UI/JS folder.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *