Find closest city via GPS coordinates - algorithm - javascript

I am given coordinates for my location (55.1858, -162.7211). I would like to find the city I am at (or near) if it exists in my dataset. The dataset (tens of thousands of coordinates) has all cities that I am interested in, and one or more coordinates corresponding to that city (depending on the size of the city):
Cold Bay, Alaska, 55.1858,-162.7211
False Pass, Alaska,54.8542,-163.4113
King Cove, Alaska, 55.0628,-162.3056
...
What's the best algorithm (preferably in JavaScript) to find the city that I am in (Without using any APIs, Google Maps etc..)?
I had a few ideas, but they're probably not the best as they're all brute force methods:
Draw a radius around my coordinates of a certain distance and then loop through the dataset to find if any of the existing coordinates are in this radius. If one or more are, then loops through them and see which I am closest to via their distance.
Somehow, start to round my coordinates at the furthest decimal place and check after each rounding if this new set of rounded coordinates exists in the dataset.
I feel like these are really bad ideas and would love some guidance or recommendations on good algorithms for this type of searching.

Related

If I have a GeoJSON file of a certain city's wards, how would I figure out which ward I'm in based on my lat/long coordinates?

Apologies in advance if this might seem like a silly question with an easy answer, or if its too vague, but I really am very new to the whole Geocoding shabang so I might not be able to ask the best questions yet.
Let's say I have the GeoJSON file for Philadelphia (https://hub.arcgis.com/datasets/phl::political-wards/explore?location=39.965479%2C-75.035211%2C11.78). I input a pair of coordinates for the Philadelphia City Hall, for example:
Latitude: 39.953306°
Longitude: -75.163577°. What would I need to do to get back the exact ward those coordinates lie in? In JavaScript (Node.js) ideally but any language would work as long as I understand the logic and steps I'd need to take.
Also, feel free to direct me to any resources/articles/videos/etc that would be useful to help me learn more about this kind of stuff.
If you have a polygon (a mathematical one, what GeoJSON would call a linear ring), and a point, imagine a line from the point to the first coordinate of the polygon. As you move the end of your line to the second coordinate of the polygon, your line moves by some angle (might be positive, might be negative, might be zero). Similarly from the second to third and so on. Add up the angles: if the result is +/-360 degrees, your point is inside the polygon. If zero, then outside. Search "winding number" for details.
Of course, it would be inefficient to test all polygons, so you make some kind of index of the bounding boxes of the polygons, and only inspect those whose bounding box contains your point, these are called R-trees (there are many variants, and lots of software available for this).

a library that assumes spherical earth and let me divide map to squires?

I want to calculate distance between two geo coordinates but because of hardware usage (cpu, ram, database usage) I don't want to be specific instead I want to divide map to squires of my choice for example 1 Km * 1 km squires is there any library out there to do this pereferly for java-scrip language I couldn't find any
ps library to calculating distance between this two squires
thank you very much for your time
If you want to slightly obfuscate the user's location, then rather than assigning them to a grid, you might effectively do the same thing by rounding off their latitudes and longitudes to however many decimal places you like then computing the distance using a more traditional Haversine Formula.
This post Calculate distance between... has many such implementations, but the one you want (javascript wise) is from #Salvador Dali.
If you are still looking to move forward with the "squire" concept, then check out http://what3words.com/ as they seem to be doing something similar to what you are looking to do though perhaps at a higher level of precision and they have an API.

Get distance to walk in Google Maps

I'm trying to do some experiment with an app. I want that, given my location and given a distance, I get a point in Google Map to which I could walk that distance.
That has some complications since getting a point X miles out from you is "easy" but it's unlikely you'll actually walk that amount of miles unless you can actually go in a straight line.
The approach I followed is using google.maps.places.PlacesService and the function nearbySearch given a radius. Then I process the distance to each returned point and luckily I get one point I could walk that distance far.
However, I've been trying some edge cases, like picking a road near a desert and then it won't work cause "places" are points of interest and that may be only just a plain road with nothing else than boring bushes for a long, long way.
In my mind it just should follow that road for the given distance and then stop.
To be clear:
I want to know from my starting point, which would be my ending point if I were to walk 1 Km
Do you have any better approach to this kind of situations?
The google.maps.DirectionsService class will calculate the route a traveller would take, given an origin and a destination. It accepts a google.maps.DirectionsRequest object. The DirectionsRequest has an option called TravelMode, which will accept any of four constants (BICYCLING, DRIVING, TRANSIT, WALKING) from the google.maps.TravelMode class.
To find a point, say, 3 kilometers from your location, I would:
Request walking directions from DirectionsService, using your current location and a point approximately 3 kilometers away from you.
Iterate over the overview_path, continually adding the length of the current segment to your grand total.
Cut the last segment to length when you exceed 3 kilometers, creating a length whose final length is very close to 3 kilometers.
I made you a Plunk demonstrating how this could be done.

how to find latlong from distance?

I want to calculate the estimated location. Suppose i have source (A) and destination (B). Let say vehicle take 18 hours to reach from source to destination. After one hour it departs, the vehicle is at point C. At point C i have speed, Lat Long and distance cover from source (A). let say it cover 100km in 2 hours at point C. At this point i want to calculate the estimated location i.e where the vehicle will be after 2 hours or what will be the location after 300 km from point C with respect to current time,speed ,location and distance at point C. Vehicle is moving along the road. Please help me in this regard. Thanks
Not actual code but some possible hints to the answer. That's how I would do it.
I assume that you use the Google Direction API and that you know how to calculate a distance between two points from their coordinates.
From this, my idea would be call the API to get your route and to the use the polyline part of the answer. The polyline is encoded with this algorithm. You can use this javascript code to get your list of points.
You then calculate the distance between each per pair of points from the start. When you reach the distance from point A to point C, you know on which segment of the polyline your vehicle should be. To get the exact coordinates on the segment, I suggest you use the interpolate function of the Google Maps Geometry API.
Of course, if your route contains many segments, you might want to use a heuristic such as approximating the search of the middle segment by filtering your list by using a box of coordinates.

Calculating distance from latitude, longitude and height using a geocentric co-ordinate system

I've implemented this method in Javascript and I'm roughly 2.5% out and I'd like to understand why.
My input data is an array of points represented as latitude, longitude and the height above the WGS84 ellipsoid. These points are taken from data collected from a wrist-mounted GPS device during a marathon race.
My algorithm was to convert each point to cartesian geocentric co-ordinates and then compute the Euclidean distance (c.f Pythagoras). Cartesian geocentric is also known as Earth Centred Earth Fixed. i.e. it's an X, Y, Z co-ordinate system which rotates with the earth.
My test data was the data from a marathon and so the distance should be very close to 42.26km. However, the distance comes to about 43.4km. I've tried various approaches and nothing changes the result by more than a metre. e.g. I replaced the height data with data from the NASA SRTM mission, I've set the height to zero, etc.
Using Google, I found two points in the literature where lat, lon, height had been transformed and my transformation algorithm is matching.
What could explain this? Am I expecting too much from Javascript's double representation? (The X, Y, Z numbers are very big but the differences between two points is very small).
My alternative is to move to computing the geodesic across the WGS84 ellipsoid using Vincenty's algorithm (or similar) and then calculating the Euclidean distance with the two heights but this seems inaccurate.
Thanks in advance for your help!
I've just worked out what seems to be the main cause of the problem. I had the latitude and longitude round the wrong way in my transformation function.
Trap for young players: Point data gives the longitude first, not the latitude.
I'm now getting 42,476.75 from my algorithm and 42,476.69 from the spheroid. Close enough for my purposes.
Thanks everybody!
Javascript is easily accurate enough in its calculations, so I don't think your problem is coming from there. Certainly not a 2.5% error or so.
You throw around words I've never even heard of, so I'll assume you're at least as knowledgeable on geodesic distance calculation as I am. I remember dabbling with this a long time ago and the way to do this required calculating hyperbolic sines and cosines to do the "weird" spherical geometry. If you just "do" Euclidian planar distance, your distances will be off once the Earth's curvature becomes significant.
So... are you doing hyp-sin's? Is your program doing exponentials and logarithms and stuff? If not, you may be applying the wrong formulae.
There... that's all I know about the topic. Good luck!
You could use the Google Maps Api for a calculation like this. I did this once.

Categories

Resources