8

I have geo location of vehicles and my point in the city and I need to find those vehicles that are within 5kms radius of my point. I could represent the vehicle locations and my point with S2 cell id. But how can i query ?

can I store data for all the users in a database and make query on S2 cell ids. As S2 cell id is using Hilbert curve, Can we know those vehicles which have closer S2 cell ids are closer in distance with each other. Or is there any other method which i have to use here to perform the search operation?

Loyalist1
  • 120
  • 1
  • 7

1 Answers1

14

I would break this problem up into several steps:

  1. Pick an appropriate S2-Level for your application. In your case, since you're querying by 5 KM radii, I'd pick level 13 cells, that have an average size of 1.27 km^2.

  2. Generate a level-13 cell covering of the 5 KM radius around the person.

  3. Get a level-13 cell from the lat/lng of the car.

  4. Do a contains check of the car S2 Cell to the 5 KM radius S2 Cell covering.

Here is an example with the Node.js JavaScript S2 Library:

const s2 = require('@radarlabs/s2');

# s2 cell level of ~1.27 km^2
const level = 13;

# cell covering of enclosure around a person
const enclosureLLs = [
  [40.77933906065449, -73.96983146667479],
  [40.77933906065449, -73.9634370803833],
  [40.78483079505022, -73.9634370803833],
  [40.78483079505022, -73.96983146667479],
].map((latlng) => {
  const [lat, lng] = latlng;
  return new s2.LatLng(lat, lng);
});

const enclosureCells = new Set(s2.RegionCoverer.getCoveringTokens(enclosureLLs, { min: level, max: level }));
# -> Set { '89c25894', '89c2589c' }

// arbitrary vehicle lat longs

const vehicle1 = new s2.CellId(new s2.LatLng(40.78340103809933,  -73.96515369415283)).parent(level);
# -> '89c2589c'

const vehicle2 = new s2.CellId(new s2.LatLng(40.782848623761375, -73.95506858825684)).parent(level);
# -> '89c258a4'


console.log(enclosureCells.has(vehicle1.token()));
# -> true

console.log(enclosureCells.has(vehicle2.token()));
# -> false

You can visualize what this looks like with Sidewalk Lab's S2 map tool:

J Kao
  • 2,023
  • 2
  • 15
  • 16
  • Thank you so much Kao. Great Logic. – Loyalist1 Sep 05 '19 at 12:42
  • Hey Loyalist1, if you found this useful, I'd appreciate it if you accepted this as the answer and upvoted it. Cheers! – J Kao Sep 09 '19 at 15:44
  • @Jkao as a S2 expert any thoughts on my question - https://github.com/google/s2geometry/issues/80 ? – gansub Sep 14 '19 at 08:54
  • @JKao similar question here - https://gis.stackexchange.com/questions/333150/unable-to-retreive-neighbors-with-geohash-algorithm-provided-in-another-answer – gansub Sep 14 '19 at 09:01