cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Drewz
Helper II
Helper II

Map multiple points within distance of multiple sites

Hi All,

I have a table of crime stats and a table of CCTV sites, see example tables below. I'd like to map crimes around each CCTV site, for example within 500 m of each site as shown below (although not with the site labels, that was just for clarification), 

 

I can apply the haversine calculation (as shown be Phil Seamark in image below). I assume the best option is to add a calculated column to the crime table. I tried below, but I get a range of errors. I'll also need to add criteria to select the closest CCTV site in case there are two with 500 m, but thought I'd focus on the first part. 

 

Any thoughts on my approach or my calculated column?

 

CCTV Sites =
IF(
CALCULATE(
12742000 * ASIN((SQRT(0.5 - COS((CCTV(Latitude)) * DIVIDE( PI(), 180 ))/2 + COS(CCTV(Latitude) * DIVIDE( PI(), 180 )) * COS(Crime[Latitude] * DIVIDE( PI(), 180 )) * (1-COS((Crime[Longitude]- CCTV(Longitude) * DIVIDE( PI(), 180 )))/2)))  < 500), CCTV (ID),"")

 

Drewz_1-1660417940622.png

 

CCTV Installaton  
Camera IDLongLatInstallation Year
201-36.9722174.78672019
202-36.9651174.7952017
203-36.9665174.80852018
204-36.9519174.83852020
205-36.9656174.81562019

 

 

Crime Stats    
IDLongLatCrime TypeYearCalculated Column
1174.7902527-36.97949982Property2015 
2174.7902527-36.97949982Injury2021203
3174.8324585-36.96062088Cyber2015 
4174.8324585-36.96062088Injury2017 
5174.8353424-36.93717957Injury2020 
6174.8353424-36.93717957Injury2016201
7174.8353424-36.93717957Cyber2020 
8174.8505554-36.9417305Cyber2019 
9174.8505554-36.9417305Cyber2018 
10174.8505554-36.9417305Property2018 
11174.7889252-37.00097275Injury2020205
12174.7889252-37.00097275Property2016 
13174.7889252-37.00097275Property2018 
14174.8352814-36.93664932Injury2019 
15174.7895508-36.97363281Injury2018202
16174.7895508-36.97363281Property2016202
17174.7895508-36.97363281Property2017 
18174.8317566-36.96046066Injury2022 
19174.8317566-36.96046066Cyber2021 
20174.8399353-36.94024658Injury2017 

 

Drewz_5-1660419372213.png

 

 

 

 

 

11 REPLIES 11
lbendlin
Super User
Super User

see message 6

Ahh, yes I did miss message 6. With large data sets, I'm getting some unusual banding when I specify distance of 200 m (see image below). Also, the haversine formula that you used gives quite different distance results. At least a factor of 2 different. I thought they would be similar. I checked the original formula by measuring a distance of Google Maps, and it gave me a matching result within 1 decimal place.  

 

Drewz_0-1660636298025.png

 

I refactored the formula to its more common format - see if that is better. I also included a simpler version that gives the same result.

 

 

DC2 = 
var a = summarize(CCTV,CCTV[Camera ID],CCTV[Lat],CCTV[Long],"Dist",12742000 * asin(sqrt(power(sin(RADIANS(CCTV[Lat] - Crimes[Lat]) / 2), 2) +  power(sin(RADIANS(CCTV[Long] - Crimes[Long]) / 2), 2) *  cos(RADIANS(CCTV[Lat])) * cos(RADIANS(Crimes[Lat])))))
var b = TOPN(1,a,[Dist],ASC)
return CONCATENATEX(b,[Dist])

DC3 = 
var a = summarize(CCTV,CCTV[Camera ID],CCTV[Lat],CCTV[Long],"Dist",6371000 * acos(sin(RADIANS(CCTV[Lat]))*sin(RADIANS(Crimes[Lat]))+cos(RADIANS(CCTV[Lat]))*cos(RADIANS(Crimes[Lat]))*cos(RADIANS(CCTV[Long]-Crimes[Long]))))
var b = TOPN(1,a,[Dist],ASC)
return CONCATENATEX(b,[Dist])

 

 

Your banding is likely caused by picking a column from the wrong table.

Sorry for the delay responding. I've been testing the above. I was getting some spurious results, so I stuck with the original haversine formula. That combined with your "brut force it" method and a distance slicer worked. So, thanks very much for your help.

 

Ideally I would like to simplify the code so I only have one calculted column that returns the CCTV ID when less than 500 m away. So if anyone has any suggestions to refine the code it would be greatfully received. Otherwise I'll keep trying to figure out some options.

 that returns the CCTV ID when less than 500 m away. 

You can only decide that after you have computed the distance.So there is no advantage in throwing away the result. Imagine you then want to say "less than 1000m" etc.  Keep the actual values.

 

One thing you can consider is to ditch the Haversine and do the simple flat earth computation based on the average meters per degree for your latitude and longitude area.  Much easier to compute.

lbendlin
Super User
Super User

1. what are the errors?

2. wouldn't you want this as a measure, rather than a calculated column?

 

Hi Ibendin,

It's not recognising the references for longitude / latitude for each table. Even for the Crime[Latitude] and Crime[Longitude] is seems to be wanting a measure. I put a screen shot below. I think it's something to do with the fact that the tables are unrelated and I need to do a row by row operation. 

 

I would have thought a calculated column would be better for a row by row operation. Also, it can process the information during the refresh which means it should respond quicker (my basic understanding). But obviously my DAX isn't right. 

 

Basically I just want to add a CCTV site in the calculated column if it's within a certain radius of the crime location. In my head it makes sense, but I'm not sure how to get it to work with unrelated tables.  

 

Drewz_0-1660534131311.png

 

You can combine both tables. Add a Location Type identifier to distinguish between cameras and crimes.

 

Combined = UNION(SELECTCOLUMNS(CCTV,"Lat",[Lat],"Lon",[Long],"Location Type","Camera","Crime Type",BLANK(),"ID",[Camera ID]),SELECTCOLUMNS(Crimes,"Lat",[Lat],"Lon",[Long],"Location Type","Crime","Crime Type",[Crime Type],"ID",[ID]))
 
I also see a very interesting error message - Your CCTV sample table has the wrong column headers.
lbendlin_0-1660566732680.png

 

 see attached

Hi Ibendin,

thanks for the example. I'm less worried about displaying the CCTV location and more interested in selecting the crimes within a certain radius based on the haversine calculation. Eventually I want to campare crime rates within a radius of CCTV locations vs crime rates beyond that radius to see if there is a difference. I think to do that, I'll need to have a calculated column in the crime table that adds a CCTV site if its within a radius. Any thoughts. 

 

Oh, and thanks re the headers.

 

Cheers Andrew

I would brute force it , calculate the distance from a crime location to all camera locations and then pick the closest.  Storing that distance will probably be useful too.

 

see attached for a possible implementation.  Note that DAX has a RADIANS function.

Note that the Haversine distance is rather meaningless in an urban environment.

Lol @ "brute force it". I'm open to anything. although for rural locations the nearest CCTV location may be miles away. But I could then filter them out. So how would "brute force it" look? Would it be a calculated column added to the Crime table?

Helpful resources

Announcements
September Update

Check it Out!

Click here to learn more about the September 2022 updates!

Power BI Show episode 9

The Power BI Community Show

Watch the playback when Priya Sathy and Charles Webb discuss Datamarts! Kelly also shares Power BI Community updates.

Power BI Dev Camp Session 26

New Date - Check it Out!

Mark your calendars and join us on Thursday, October 6 at 11a PDT for a great session with Ted Pattison!

Top Solution Authors