COGNOiSe.com - The IBM Cognos Community

IBM Cognos Analytics Platform => Cognos Analytics => Reporting => Topic started by: cadams on 05 Mar 2018 07:16:16 PM

Title: Latitude and longitude calulations
Post by: cadams on 05 Mar 2018 07:16:16 PM
OK tech heads,

With the advent of Cognos 11.0.8 we have the ability to report using maps with Latitude and Longitude, I have a requirement to list coordinates that are X distance from point A.

so for example I work at 38.8977° N, 77.0365° W, Donald has a headache, and would like to know all the pharmacies within 5 miles.
Title: Re: Latitude and longitude calulations
Post by: aussieadam on 06 Mar 2018 08:56:14 AM
if you have longitude/latitudes of all pharmacy places it's pretty simple with a calculation.
I have personally done this in a function on our database. would just have to look at distance returned <= 5.
CREATE OR REPLACE FUNCTION latlon_distance(
    lat1 numeric,
    lon1 numeric,
    lat2 numeric,
    lon2 numeric)
  RETURNS numeric AS
$BODY$
DECLARE   
   radius numeric;
   dlon numeric;
   dlat numeric;
   a numeric;
   c numeric;
   d numeric;
BEGIN
   dlon = lon2-lon1;
   dlat = lat2-lat1;
   radius = 3961;
   
   a = (sin(radians(dlat/2)))^2 + cos(radians(lat1)) * cos(radians(lat2)) * (sin(radians(dlon/2)))^2;
   c = 2 * atan2(sqrt(a), sqrt(1-a));
   d = radius * c;
   return d;
   END;
$BODY$
  LANGUAGE plpgsql VOLATILE;

Honestly unsure if cognos supports sine and cosine, but if your database does you should be able to figure out a way to get that in a filter.
Title: Re: Latitude and longitude calulations
Post by: dougp on 06 Mar 2018 10:48:04 AM
I think this isn't a Cognos question.  It's a question regarding filtering the data by using spatial functions in the database.  You'll probably want to use a stored procedure or view.

If you can't create stored procedures or views, you'll need to contact your database administrator about this.

If you can create stored procedures or views, you'll need to know what RDBMS you are using.  For Microsoft SQL Server, this information may help:
Buffer (http://"https://docs.microsoft.com/en-us/sql/t-sql/spatial-geometry/stbuffer-geometry-data-type")
Distance (http://"https://docs.microsoft.com/en-us/sql/t-sql/spatial-geometry/stdistance-geometry-data-type")
Intersects (http://"https://docs.microsoft.com/en-us/sql/t-sql/spatial-geometry/stbuffer-geometry-data-type")
Intersection (http://"https://docs.microsoft.com/en-us/sql/t-sql/spatial-geometry/stintersection-geometry-data-type")
All of those pages have a table of contents from which you can learn about other SQL Spatial functions.

I haven't had the opportunity to play with this technology much.  It may be possible to use these functions directly in the FM model.
Different combinations of these functions may produce different results and have different performance implications.