How can we calculate sea distances using waypoints? - google-maps

I have one query in which I really stuck at that. I have port database with waypoints and also routing points which I need to use in distance calculation between two ports. I have done lots of R&D to find formula which gives me distance between two points. I also need shortest route which is possible.
I have reviewed online tools which allow user to calculate the distance. But I want to do the same at my own. I have reviewed Port World Distance Calculator. I reviewed the Great Circle formula to achieve that but I don't know that how we avoid landscapes in sea distance and in which direction I need to find distance for second port.

The simpliest that comes - just create a table in your database like:
port1id : port2id : distance
number of ports are limited, and this table will give you flexibility to set correct distance

Related

Get close points (geometry) in MySQL

I have a MySQL database table with a series of points, which is a specific geometry data type (basically, a lat/lon coordinate). I need to get all the points that are close to some coordinates (close meaning to less that 1 km, let's say).
Now I am getting ALL points from the table and, from PHP, calculating which of them have a distance shorter than the desired distance (1 km).
The problem is that there a thousands of points, so the performance is very poor.
Is there a way to get only those close points directly from the database? I don't guess what function may help me.
Thank you!

Given a user's lat lng, how to find the nearest lat lng from a database of thousands of lat lng?

I have data of locations of thousands of sensors in MySQL. I want to identify the sensor closest to the user's location and show that specific sensor's data. All the location data is available as lat lng.
I understand that one approach can be to find displacements between the origin and all the sensors using Haversine formula and select the one with the shortest distance. The problem here is that there are tens of thousands of sensors.
Any suggestions/leads?
Spatial index allows efficient query of points within any specific distance. The problem of course is one might not know the search radius needed in specific case. Unfortunately, a large radius causes inefficient queries, and a small radius might result in no match at all.
A possible solution is to search with increasing radius, until the search returns some results, and then find the closest result among those.
This article describes this solution for BigQuery, would require some adaptation for MySQL script dialect:
https://mentin.medium.com/nearest-neighbor-using-bq-scripting-373241f5b2f5
Not the MySQL answer you are looking for but Postgresql's popular PostGIS extension has an inbuilt K Nearest Neighbor operator class). Also, see its documentation. It works great!
Also, I am aware of this Go library that allows you to do KNN in memory after building a Quadtree with your sensor locations.
For only thousands, a simple bounding box with two 2-column indexes may be fast enough.
For better speed, see SPATIAL indexing.
For details on those two solutions, plus two faster ones, see Find Nearest

Google maps distance matrix API: get absolute distance

I want to calculate distances between two coordinates but sometimes the origin coordinate is over / beyond the destination coordinate. And it is really problematic at one-way roads, because in this case the distance can be eg. 1 km to get back to the destination. In real life it is OK, but actually the real distance is only eg. 10 meters (if I could turn back on one-way roads). So only the direction is wrong.
I can resolve this problem if I call the API twice (origins=my_origin_coordinate&destinations=my_destination_coordinate and origins=my_destination_coordinate&destinations=my_origin_coordinate).
But is there a easier way to get both distance in one query? (to save my query limit...)
The distance matrix allows multiple results in a single query. You can do origins=my_origin_coordinate|my_destination_coordinate&desti‌​nations=my_origin_co‌​ordinate|my_destinat‌​ion_coordinate in a single request, which will give you both results. That won't really help with your quota though as the quota is based on "elements", and whether you do it in one query or two, it will be the same number of "elements".

How to cluster latitude-longitude data based on fixed radius from centroid as the only constraint?

I have around 200k latitude & longitude data points. How can I cluster them so that each clusters have latitude & longitude points strictly within radius = 1 km from centroid only?
I tried leadercluster algorithm/package in R but eventhough I specify radius =1 km its not strictly enforcing it i.e. its give clusters with lot of point say 5 - 10 kms from cluster centroid also within the same cluster. So its not meeting my requirement.
Number of points in a cluster can vary & its not problem.
Is there a way to enforce the strict radius constraint in heirarchical or another clustering algorithm? I am looking for the steps & implementation in R/python.
I tried searching in stackoverflow but couldn't find a solution in r/python.
How to visualize cluster centroids in google maps after the clustering in done?
EDIT
Parameters I am using in ELKI. Please verify
This is not so much a clustering, but a set cover type of problem. At least if you are looking for a good cover. A clustering algorithm is about finding structure in your data; but you are looking for some forced quantization.
Anyway, here are two strategies you can try e.g. in ELKI:
Canopy preclustering with T1=T2=your radius. This should yield a greedy approximation to the cover scenario.
Complete linkage hierarchical agglomerative clustering, cut at the desired height. This is fairly expensive (O(n^3)). Any two points in the same cluster have at most this distance, so this is a bit stricter than your requirement.
Beware that you should be using haversine ("geo") distances, not Euclidean!

Is there any MySQL function to directly get 5 closest coordinates to a given coordinate from database?

I am working with PHP and use MySQL for database. I need a way, to get 5 closest coordinates to a given coordinate from database, which is very fast and at least 80-90% accurate. I have researched a lot. I found havershine formula, spherical law of cosines, bounding square method to compare min and max latitude-longitude values with coordinate in database and other methods which use trigonometric math functions. But all these formulas take a long to return result in database with thousands of entries. Does MySQL provide any function to do it fast?
See this similar question on the GIS Stack site. The performance of your ultimate solution will depend on how many targets are in the reference table you are searching and if you can limit the distance you are interested in (such as closest 5 within 30 miles). I don't think you can reliably optimize the process; you need to calculate the distance for all coordinates in your reference table.