10 Replies Latest reply on Sep 5, 2018 7:59 PM by Alex Kerin

# Calculating/Mapping Lat+Long Points within Polygon/Geofence

Hello!

I'd love to know if Tableau is able to calculate or map how many lat/long points, within a given dataset, reside in a given map polygon/geofence.

Context: Assume I have a CSV file with the following type of data.

UseridOriginLatitude
OriginLongitudeDestinationLatitude
DestinationLongitude
1

 37.7904

 -122.422

 37.7937

 -122.403
2

 37.7867

 -122.414

 37.7864

 -122.408
3

 37.7871

 -122.411

 37.8084

 -122.41

I also have a GeoJSON .json file with a few geofence polygon outlines. I'm trying to find out how many of these lat/long points in the csv file, exist within any of the geofence polygons. I can also have a .kml file with these polygons.

Is this possible? Or would I somehow have to map each of these csv rows to a polygon, for Tableau to understand.

Any other tools/solutions are welcome. Thank You!

• ###### 1. Re: Calculating/Mapping Lat+Long Points within Polygon/Geofence

I think you're in for a tough ride in Tableau alone - if you were just using rectangles or circles it would be much easier. I doubt your polygons are as complex as these, but take a look at this article: Determining Whether A Point Is Inside A Complex Polygon, linked from here: A Simple Geo Fencing Using Polygon Method - CodeProject, and more generally here: Point in polygon - Wikipedia

• ###### 2. Re: Calculating/Mapping Lat+Long Points within Polygon/Geofence

Hi David Yao,

Using Tableau 2018.2 - this process is much simpler.

1. You can convert the csv file to a shapefile using QGIS tool "points layer from table" and saving as a Shapefile

2. Using Spatial Join in 2018.2 - join the Geometry of the polygon boundary with the point geometry from the new Shapefile using the predicate "Intersects"

Best,

Kent M.

@datakmart

3 of 3 people found this helpful
• ###### 3. Re: Calculating/Mapping Lat+Long Points within Polygon/Geofence

Hi Kent,

Running into a visualization issue:

I have a .json file with the polygons, and a .geojson file with the points that are inner joined with "intersects".

The resulting table appears to correctly match the Geometry of POLYGON to POINT.

However, when I go to a worksheet to try and visualize everything together, it fails.

Putting in the POINT lat/long works and displays the map as expected, but when I add in the Latitude (generated) and Longitude (generated) measures associated with the POLYGON file, the map disappears and the points exist in an X/Y graph instead.

Using dual axis doesn't fix that either. How can I go about visualizing the polygons with point lat/longs?

Thank you!

• ###### 4. Re: Calculating/Mapping Lat+Long Points within Polygon/Geofence

Hi David Yao,

What version of Tableau are you using?

Can you share the files you are using?

Thanks,
Kent M.

2 of 2 people found this helpful
• ###### 5. Re: Calculating/Mapping Lat+Long Points within Polygon/Geofence

Hi Kent,

[Edited this, as I found a way to add an identifier to the polygon geojson file, so that Tableau can aggregate against that.

Can you verify that this is, in general, how to do it?

Also, is it possible to display both the individual lat/long points, along with the actual polygons? I tried to dual axis and the map disappeared. Thanks!]

I'm using the latest, 2018.2.0 (20182.18.0627.2230) 64-bit.

Sure, here are the two files.

1. test_csv.geojson, is a sample csv file of lat/long points I converted to a geojson file.

2. overlay_group.geojson is a geojson file I exported from a polygon making tool online (ScribbleMaps). I added a few sample polygons from San Francisco.

I'm trying to answer how many lat/long points exist in each of these polygons I've created, in San Francisco.

Any help would be much appreciated!

Thanks!

David

1 of 1 people found this helpful
• ###### 6. Re: Calculating/Mapping Lat+Long Points within Polygon/Geofence

Hi David,

You need a unique identifier for the points to disaggregate, otherwise you have to turn off Aggregate Measures.

Below are examples of how it can work after creating the join.

Best,

Kent M.

3 of 3 people found this helpful
• ###### 7. Re: Calculating/Mapping Lat+Long Points within Polygon/Geofence

Hey Kent,

Thanks so much! It's been great being able to aggregate within these polygons.

I have another question if you wouldn't mind!

Let's say these lat/long points are pickup and drop-off points within this city.

So, each row will have:

• trip_order {1,2}. Defines whether the point is a pickup or drop-off.
• trip_id {1,2,3,4...}. Associates two pickup and drop-off points, together.

I'm trying to determine and visualize, which pickup and drop-off pairs exist in each polygon or groups of polygons.

Is this possible? I'm assuming I would need to create a calculated field, based on whether or not for each pair, the POINT geometry exists in the POLYGON geometry columns.

Thank You!

David

• ###### 8. Re: Calculating/Mapping Lat+Long Points within Polygon/Geofence

Hi David,

After you do the spatial join, you may be able to use a Level of Detail calculation to determine which pairs exist in each polygon.  Something like this might work (I haven't actually re-created the twbx to try this out, but based on the images of the tables in Kent's example above...)

{fixed Id, trip_id: sum(number of records)} - I think what this should give you is for every row, you'll know how many points with the same trip ID fall inside the same polygon (based on the 'Id' field).  If the value is 1, there is only an origin *or* a destination.  If the value is 2 then there is both an origin and a destination (assuming that there aren't multiple pick-ups and drop offs on any given route).

-Sarah.

Tableau Research

3 of 3 people found this helpful
• ###### 9. Re: Calculating/Mapping Lat+Long Points within Polygon/Geofence

Hi Sarah!

Thank you so much for the response. LOD expressions were exactly what I needed!

Ended up using the above calculation then dividing by 2 to get the true number of unique trips that start and end in each polygon.

Then for trips that start and end across multiple polygons, used the following, {FIXED [Trip_ID], [ID] = '1' OR [ID] = '2' OR [ID] = '3': SUM([Number of Records])}

Thanks to you and Kent again!

-David

• ###### 10. Re: Calculating/Mapping Lat+Long Points within Polygon/Geofence

This thread (and by extension, Tableau and the users here) is amazing - Giving the complexity of doing this by the methods I linked to at Wikipedia, I never would have imagined that 2018.2 could achieve this with relative simplicity.