One of the challenges I have faced recently for one of our customers is implementing a piece of code within some Web Dynpro ABAP that would work with their installed geo-coding solution which would, based on a user’s home location and office location, give an accurate driving distance between the two addresses. Now this doesn’t sound too difficult and in fact, like everything else, it is not (once you know how!).
SAP already has in place, a set of function modules with which you can get the distance between two geo-coordinates as the crow flies, which is fine as long as you are not relying on the output to give an accurate representation of how long it will take you to get from A to B. However in most cases more is required. So how do we achieve geo-routing?
Firstly, without going into too much detail, make sure that you have installed and configured your geo-coder. Next we will need to know the RFC destination. This can be found easily by going to the transaction SPRO.
A geocoding program is first registered in the system and then assigned to a country. Assuming this has been done, we only need to know at this stage which geo-coder we are going to use and what the name of the RFC destination for that geo-coder is. This can be found here using the nodes shown above. In this example our geo-coder is IGS1 and our RFC destination is IGS_RFC_DEST1. We can see this in the “Register Geocoding Program in the System” node.
For the purposes of this blog we are going to assume that the country GB has been configured to use the geo-coder IGS1. Now that we have the location of our geo-coder we can now start to code our solution.
Firstly we need to turn our two addresses into longitude and latitude. This is done using the function ‘FITP_WEB_GEOCODING_GET_SINGLE’ which accepts a structure of the type FTPS_WEB_ADDRESS containing the following attributes:
This will then return a structure of the type FTPS_WEB_GEOCODING which is simply the longitude and latitude of the address you provided.
Once you have completed this for both addresses you are ready to compute the distance. You will first need to declare an object of type ref to class CL_GIS_ROUTE_IGS which can be used for this task. When you create this object you will need to give it a name and you will need to set the RFC destination to the one we identified earlier. This is shown below:
DATA: LO_GEOROUTE TYPE REF TO CL_GIS_ROUTE_IGS.
CREATE OBJECT LO_GEOROUTE
name = ‘IGS1’.
LO_GEOROUTE->M_RFC_DESTINATION = ‘IGS_RFC_DEST1’.
Now we need to add the address structures to the route as stops. This is done by using the ADD_STOP method which requires a structure of the type GEOPOSITION containing the Longitude and Latitude of the address and a unique ID of type C to uniquely identify the location. See example below:
CALL METHOD LO_GEOROUTE->IF_GIS_ROUTE~ADD_STOP
ID = ‘Office’
POSITION = ls_addr1.
Repeat this step for both addresses. Following this we need to execute the geo-routing for this segment.
CALL METHOD LO_GEOROUTE->IF_GIS_ROUTE~EXECUTE.
Finally we can retrieve the distance computed by the geo-coder. We are only dealing with two addresses so we are going to be retrieving the distance for part 0 of the journey (A to B).
CALL METHOD LO_GEOROUTE->IF_GIS_ROUTE~GET_PART_INFO
PART = ‘0’
DISTANCE = lv_distance .
The distance returned here is of the type GEODIST. This solution will need to be fleshed out with error checking and any other requirements but assuming that your addresses exist and your system is configured correctly, you will now have a fully functioning, distance calculating geo-router.