Multi Model - Spatial
Objective
With SAP HANA Cloud’s spatial engine, application builders can add spatial intelligence to business data to gain deeper insights, identify new opportunities and discover root causes of complex issues.
Some key features include:
- Native storage for 2D, 3D and 4D vector data types
- 100+ native, in-memory, geospatial functions that integrate into SQL
- Vector Tile integration
- Compliant with open standards for spatial data formats
- Esri-supported enterprise geodatabase
- Open-source plugins (GeoServer, QGIS)
- Python interface for data science

Try it out!
Viewing the State map
This exercise uses the dataset from the GX_WORLD_COUNTRIES table to highlight borders of a country.
- Switch to the SAP HANA Database Explorer.
To display spatial data on a map, the default byte limit in the Database Explorer must first be increased.
-
Proceed as follows:
- Switch to Settings.
- Open the SQL Console tab.
- Change the Byte limit for Large Objects (LOBs) to 1000024.
- Choose Save.

-
Exit the settings and switch back to the Database Explorer. Select Tables and search for GX_WORLD_COUNTRIES.
-
Right-click on GX_WORLD_COUNTRIES and select Open Data.

- Scroll down to Germany and double-click on the SHAPE entry or right-click and select View Spatial Data.

- The map will appear:

Viewing customers on the map
Now we want to display all customers from Germany. To do this, execute a database query and consider the points individually.
- Select the SQL Console icon to open a new SQL console, then copy the below SQL statement and choose Run.
1SELECT * FROM GX_CUSTOMERS;
- Notice the addresses already exist in the table as latitude and longitude. In order to use the map viewer, these numbers must be combined into a singular data type called ST_POINT using the ST_POINT function in SAP HANA Cloud. Paste the following SQL command into the console and execute it:
12345678SELECT
CUSTOMER_ID,
NEW ST_POINT(
TO_DOUBLE(CUSTOMER_LONGITUDE),
TO_DOUBLE(CUSTOMER_LATITUDE),
1000004326
) AS LOCATION
FROM "GX_CUSTOMERS";
- Double-click on a LOCATION entry which will render a map displaying the customer location:

The next step is to include this logic within a database view for ease of reuse.
- Copy and paste the SQL statement below into the SQL Console, then execute the statement.
12345678CREATE VIEW "VW_CUSTOMER_LOCATION" AS
SELECT CUSTOMER_ID,
NEW ST_POINT(
TO_DOUBLE(CUSTOMER_LONGITUDE),
TO_DOUBLE(CUSTOMER_LATITUDE),
1000004326
) AS CUSTOMER_LOCATION
FROM "GX_CUSTOMERS";
Viewing all customers on the map
Now it is possible to query all customer locations as a collection of points.
- To display all customer locations at the same time, the points must be combined into a single object called a geometry collection. The following command gathers all the points from the database view for display by the map:
12SELECT ST_CollectAggr(CUSTOMER_LOCATION)
FROM "VW_CUSTOMER_LOCATION";- Double-click on the resulting output row, or right-click and select View spatial data the option on the result:

- A map displaying all the customer locations appears:

Separating customer addresses to a specific area
For this use case, reduce the set of customers to those only in Germany. Using the polygon from GX_WORLD_COUNTRIES, we can check which points are within a certain border. To further simplify processing, update the previously created view.
- Switch back to SQL Console, copy, paste and execute the following SQL statement:
12345678910111213141516171819drop view VW_CUSTOMER_LOCATION;
CREATE VIEW "VW_CUSTOMER_LOCATION" AS
SELECT CUSTOMER_ID,
NEW ST_POINT(
TO_DOUBLE(CUSTOMER_LONGITUDE),
TO_DOUBLE(CUSTOMER_LATITUDE),
1000004326
) AS CUSTOMER_LOCATION
FROM "GX_CUSTOMERS",
"GX_WORLD_COUNTRIES"
WHERE "COUNTRY" = 'Germany'
AND "SHAPE".ST_Contains(
NEW ST_POINT(
TO_DOUBLE(CUSTOMER_LONGITUDE),
TO_DOUBLE(CUSTOMER_LATITUDE),
1000004326
)
) = 1;- Now execute the following statement to organize all customers in Germany as a collection bordered by the German border (the filter condition of the SQL View checks whether the locations are in Germany).
1234SELECT "SHAPE".ST_Collect(ST_CollectAggr(CUSTOMER_LOCATION))
FROM "VW_CUSTOMER_LOCATION",
"GX_WORLD_COUNTRIES"
WHERE "COUNTRY" = 'Germany';
- Double-click on the result, or right-click and select View spatial data to view the resulting map:

Clustering
Using spatial processing, view the clustering of all customer locations in Germany.
- Switch back to SQL Console, copy, paste and execute the following SQL statement to create a new database view.
1234567CREATE VIEW "VW_SPATIAL_CLUSTERING" AS
SELECT ST_ClusterID() AS ID,
ST_ClusterCell() AS SHAPE,
ST_ClusterCell().ST_PointOnSurface().ST_AsText() AS POINT,
COUNT(*) AS COUNT
FROM "VW_CUSTOMER_LOCATION" GROUP CLUSTER BY "CUSTOMER_LOCATION" USING HEXAGON X CELLS 20
ORDER BY COUNT DESC;- Check under the Views area of the Catalog to verify that the View is visible.

Now look at the cluster result from different views.
- In Database Explorer, right-click on the VW_SPATIAL_CLUSTERING view and select Open Data.

- Look at the largest hexagon with 200 entries by double-clicking the SHAPE entry. The result for the highest customer density is in an area around Frankfurt:

Check out some other entries to get the customer densities for different locations!
Further information
With SAP HANA Cloud’s spatial engine, application builders can add spatial intelligence to business data to gain deeper insights, identify new opportunities and discover root causes of complex issues.
Some key features include:
- Native storage for 2D, 3D and 4D vector data types
- 100+ native, in-memory, geospatial functions that integrate into SQL
- Vector Tile integration
- Compliant with open standards for spatial data formats
- Esri-supported enterprise geodatabase
- Open-source plugins (GeoServer, QGIS)
- Python interface for data science

Try it out!
Viewing the State map
This exercise uses the dataset from the GX_WORLD_COUNTRIES table to highlight borders of a country.
- Switch to the SAP HANA Database Explorer.
To display spatial data on a map, the default byte limit in the Database Explorer must first be increased.
-
Proceed as follows:
- Switch to Settings.
- Open the SQL Console tab.
- Change the Byte limit for Large Objects (LOBs) to 1000024.
- Choose Save.

-
Exit the settings and switch back to the Database Explorer. Select Tables and search for GX_WORLD_COUNTRIES.
-
Right-click on GX_WORLD_COUNTRIES and select Open Data.

- Scroll down to Germany and double-click on the SHAPE entry or right-click and select View Spatial Data.

- The map will appear:

Viewing customers on the map
Now we want to display all customers from Germany. To do this, execute a database query and consider the points individually.
- Select the SQL Console icon to open a new SQL console, then copy the below SQL statement and choose Run.
1SELECT * FROM GX_CUSTOMERS;
- Notice the addresses already exist in the table as latitude and longitude. In order to use the map viewer, these numbers must be combined into a singular data type called ST_POINT using the ST_POINT function in SAP HANA Cloud. Paste the following SQL command into the console and execute it:
12345678SELECT
CUSTOMER_ID,
NEW ST_POINT(
TO_DOUBLE(CUSTOMER_LONGITUDE),
TO_DOUBLE(CUSTOMER_LATITUDE),
1000004326
) AS LOCATION
FROM "GX_CUSTOMERS";
- Double-click on a LOCATION entry which will render a map displaying the customer location:

The next step is to include this logic within a database view for ease of reuse.
- Copy and paste the SQL statement below into the SQL Console, then execute the statement.
12345678CREATE VIEW "VW_CUSTOMER_LOCATION" AS
SELECT CUSTOMER_ID,
NEW ST_POINT(
TO_DOUBLE(CUSTOMER_LONGITUDE),
TO_DOUBLE(CUSTOMER_LATITUDE),
1000004326
) AS CUSTOMER_LOCATION
FROM "GX_CUSTOMERS";
Viewing all customers on the map
Now it is possible to query all customer locations as a collection of points.
- To display all customer locations at the same time, the points must be combined into a single object called a geometry collection. The following command gathers all the points from the database view for display by the map:
12SELECT ST_CollectAggr(CUSTOMER_LOCATION)
FROM "VW_CUSTOMER_LOCATION";- Double-click on the resulting output row, or right-click and select View spatial data the option on the result:

- A map displaying all the customer locations appears:

Separating customer addresses to a specific area
For this use case, reduce the set of customers to those only in Germany. Using the polygon from GX_WORLD_COUNTRIES, we can check which points are within a certain border. To further simplify processing, update the previously created view.
- Switch back to SQL Console, copy, paste and execute the following SQL statement:
12345678910111213141516171819drop view VW_CUSTOMER_LOCATION;
CREATE VIEW "VW_CUSTOMER_LOCATION" AS
SELECT CUSTOMER_ID,
NEW ST_POINT(
TO_DOUBLE(CUSTOMER_LONGITUDE),
TO_DOUBLE(CUSTOMER_LATITUDE),
1000004326
) AS CUSTOMER_LOCATION
FROM "GX_CUSTOMERS",
"GX_WORLD_COUNTRIES"
WHERE "COUNTRY" = 'Germany'
AND "SHAPE".ST_Contains(
NEW ST_POINT(
TO_DOUBLE(CUSTOMER_LONGITUDE),
TO_DOUBLE(CUSTOMER_LATITUDE),
1000004326
)
) = 1;- Now execute the following statement to organize all customers in Germany as a collection bordered by the German border (the filter condition of the SQL View checks whether the locations are in Germany).
1234SELECT "SHAPE".ST_Collect(ST_CollectAggr(CUSTOMER_LOCATION))
FROM "VW_CUSTOMER_LOCATION",
"GX_WORLD_COUNTRIES"
WHERE "COUNTRY" = 'Germany';
- Double-click on the result, or right-click and select View spatial data to view the resulting map:

Clustering
Using spatial processing, view the clustering of all customer locations in Germany.
- Switch back to SQL Console, copy, paste and execute the following SQL statement to create a new database view.
1234567CREATE VIEW "VW_SPATIAL_CLUSTERING" AS
SELECT ST_ClusterID() AS ID,
ST_ClusterCell() AS SHAPE,
ST_ClusterCell().ST_PointOnSurface().ST_AsText() AS POINT,
COUNT(*) AS COUNT
FROM "VW_CUSTOMER_LOCATION" GROUP CLUSTER BY "CUSTOMER_LOCATION" USING HEXAGON X CELLS 20
ORDER BY COUNT DESC;- Check under the Views area of the Catalog to verify that the View is visible.

Now look at the cluster result from different views.
- In Database Explorer, right-click on the VW_SPATIAL_CLUSTERING view and select Open Data.

- Look at the largest hexagon with 200 entries by double-clicking the SHAPE entry. The result for the highest customer density is in an area around Frankfurt:

Check out some other entries to get the customer densities for different locations!