Quantcast
Channel: SCN : All Content - SAP BusinessObjects Predictive Analytics
Viewing all articles
Browse latest Browse all 836

Custom R Component: Google Maps

$
0
0

This component adds static Google Maps to SAP Predictive Analysis. The user can chose through parameters what locations should be displayed, their color coding, the map's zoom level and the region the map should focus on.

 

Background

Please scroll to the bottom of this page for instructions on implementing this function (R Code and Configuration). At the bottom of the page you will also find a sample dataset. The file contains a small part of a perception survey done by Eurostat in 2009 about the quality of life in 75 different European cities. The cities were clustered with this component and later enriched with geocoordinates. Even though the clustering did not know anything about the location of the cities, it is striking how the clusters are clearly visible on the geographical map.

 

Thank you very much to our R-Expert Pramila (Pramilamma Bovilla) for adding the setInternet2() function and the destfile parameter to make this script work in SAP Predictive Analysis!

 

If you are new to creating Custom R Components in SAP Predictive Analysis, you can have a look at this overview to get you started. Please note that this code is not supported by SAP. When using this function please carry out your own testing.

0.JPG

 

Usage

Load the attached dataset EuropeanUrbanAudit_WithClusterAndGeocodes.csv into SAP Predictive Analysis.

1.JPG

 

Next add the new "Google Maps" component to your model.

2.JPG

Configure the component. You need to set the following:

  • The geographic region for the map to center on.
  • Zoom Level.
  • A numerical column that controls the color coding (for instance a cluster number).
  • Whether the places should be displayed by dots or with their names.
  • The column that holds the place names (only used if the places are to be displayed by name).
  • Columns holding the latitude and longitude coordinates.

3.JPG

 

Run the model and click on 'Charts'. You can see how the clusters, that were created without any knowledge about their geographic location, are very much located together on the map! The perception of quality of life clearly has a strong relationship to the location.

0.JPG

 

If you want to know more the structure of these clusters, you can look at them in the Hierarchical Clustering component.

 

R Code

Please make sure you have the R library RgoogleMaps installed before running this code.

## Google Maps

##

## Prerequisites

## - Library RgoogleMaps is installed.

## - The data is already geocoded and holds the latitude and longitude coordinates.

##

## Version 1.00

##

## Author: Andreas Forster

## http://scn.sap.com/people/andreas.forster

##

mymain <- function (mydata, strCenter, strZoom, strColColor, strDisplay, strColPlaceName, strColLat,  strColLong)

{

library(RgoogleMaps)

 

## Enable Internet access.

setInternet2(TRUE)

 

## Retrieve and plot the plain map.

mymap <- GetMap(center=strCenter, zoom=as.integer(strZoom), destfile=paste(Sys.getenv("TMP"), '\\MyTile.png', sep=''))

PlotOnStaticMap(mymap)

 

## Add the locations/names to the map.

mycolors <-levels(as.factor(mydata[, strColColor]))

if(strDisplay=='Dot')

  {

  PlotOnStaticMap(lat=mydata[,strColLat], lon=mydata[,strColLong], cex=3, pch=20, col=mycolors, MyMap=mymap)

  }

else

  {

  TextOnStaticMap(lat=mydata[,strColLat], lon=mydata[,strColLong], cex=3 ,label=mydata[,strColPlaceName], col=mycolors, MyMap=mymap)

  }

 

## Draw legend

legend("topright", legend=mycolors, fill=mycolors, bg="white")

 

## Return input data as output.

return(list(out=mydata))

}

 

Configuration

conf1.JPG

conf2.JPG

conf3.JPG

conf4.JPG


Viewing all articles
Browse latest Browse all 836

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>