Adding points to a linestring - php

So I'm tracking a number of vessels as they travel across the ocean, creating a LINESTRING() in MySQL once I've gotten all the known GPS points from departure to arrival. Sometimes, however, I'm able to find additional points after creating the linestring, sometimes without associated time data.
So my question is: What would be the simplest way to take a linestring and add a point to it? It's reasonable to assume that, point by point, the LINESTRING doesn't change direction much and that it should be possible to deduce mathematically between which points the new point should be placed; but I can't seem to figure it out.
My solution so far finds which given coordinate on the LINESTRING is the closest, but I haven't been able to figure out the math that decides whether it should come before or after. Maybe the solution involves finding out which part of the linestring the point is closest to and then inserting it between the two coordinates that define that part of the line?

A possible answer...
Sort the GPS points by either latitude or longitude. Pick the one that has the most variation. Then construct the string that defines a line.
If the line is not "straight" or arcs in certain ways around the globe, or has to maneuver around a peninsula, or ..., the algorithm is to simplistic. Also, if the error in GPS is sometimes greater than the distance between two 'consecutive' points, the algorithm is flawed.

Related

Matrix Distance Genetic Algorithm on Android

I am making a routing application on Android when user can input the amount of hours to travel a place and my application would give an output of possible route users can travel.
I am using genetic algorithm (GA) to give the route to the user, and I use PHP to execute my GA.
Here comes the problem, in order for routing to be effective, I need to know distance between each city to verified if the route is possible and the distance in minimized. How to store distance between each city in order to make the execution faster? I have tried to get the distance directly
from Google Maps API but it takes longer execution time.
I was thinking to store the distance to json file, but is it possible? Or is there another effective ways?
Note that the destination will be dynamic. Users can add a new destination, so whenever there is a new destination the matrix distance needs to be updated.
Please help me :) Thank you.
You know the initial position of the user and want to know different destination distances. I suggest you to use single source shortest path deterministic algorithm like Dijkstra instead of evolutionary algorithm. The implementation based on a min-priority queue implemented by a Fibonacci heap running in O(E.logV) where E is the number of edges and V is the number of vertices. It runs much faster than genetic algorithm and also find the best answer instead of some approximate one. It also has the property that finds the first nearest destinations first which is suitable for you.

Finding Radius (in Miles) From an Address in MySQL Database - Where to start?

I am in the very early stages of creating a simple web-based tool for a client for internal use that can search their existing data of housing options based on certain criteria, one of which being distance from a given point. I will be using PHP and MySQL.
It's basically like an apartment locator.
The searching within a radius is the one thing I see that could pose a problem. I know this can be done easily if I had the lat/long coordinates already, but as it stands right now, I would have to convert all the addresses, and there are over 100.
The goal is to type in an address and return a list of properties X miles from that address.
Is there a way to do the lat/long conversion on the fly instead of converting all their existing property addresses to lat/long?
If I do need to do a batch conversion of addresses, so be it, but moving forward, is there a way to automate that when adding a new record?
Is there some kind of customizable apartment locator API I could use for this?
I'm not looking for any specific code, just need to be pointed in the right direction to figure out what I need to do to accomplish this.

Location/Proximity search on large record set

Say I have a database table representing users with potentially millions of records (Wishful thinking). This table contains a whole bunch of information about each user including information about their location:
City
County/State etc
Country
Latitude
Longitude
Geohash based on the latitude/longitude values.
I would like to implement a feature where by a logged in user can search for other users that are nearby.
Ideally, I would like to grab say the 20 users that are geographically closest to the user, followed by the next 20, and the next 20 etc. So essentially I want to be able to order my users table by the distance from a certain point.
Approach 1
I have some previous experience with the haversine formula which I used to calculate the distance between one point and a few hundred others. This approach would be ideal on a relatively small record set but I fear it would become incredibly slow with such a large record set.
Approach 2
I've additionally done some research into geohashing and I understand how the hash is calculated and I get the theory behind how it represents a location and how precision is lost with shorter resolutions. I could of course grab the users that are located near the user's geographical area by grabbing users that have a similar beginning to their geohash (Based on a precision I specify - and potentially looking in the neighbouring regions) but that doesn't solve the problem of needing to sort by location. This approach is also not great for edge cases where 2 users may be very close to one another but lie close to the edges of 2 regions represented by the geohash.
Any ideas/suggestion towards the approach would be greatly appreciated. I'm not looking for code in particular but links to good examples and resources would be helpful.
Thanks,
Jonathon
Edit
Approach 3
After some thought I've come up with another potential solution to consider. Upon receiving each user's location information, I would store information about the location (town/city, area, country, latitude, longitude, geohash maybe) in a separate table (say locations). I would then connect the user to the location by a foreign key. This would give me a much smaller dataset to work with. To find nearby users I could then simply find other locations that are close to the user's location and then use their IDs to find other users. Perhaps some sort of caching could be then implemented by storing a list of the nearby location IDs for each location.
You can try a space filling curve. Translate the co-ordinate to a binary and interleave it. Treat it as base-4 number. You are also wrong a geohash can be used to sort also by location. Most likely use a bounding box and filter the solution and then use the harvesine formula.

Geocode distance on a defined route

I am working on a mobile web site for a MS Bike event. I already have geo code for tagging email requests, and a check-in site to check riders in to a location based on their location. I would like to add the distance to the next rest stop / finish. I know how to figure out the distance between two locations. And all my research on this, it to allow Google to provide the route. But since this is an event, there is a predetermined route that the riders ride.
Does anyone have any ideas on how to tackle this? I have the Lat/Long of the routes (each corner and turn) and I have it in a kml format.
If the resolution of the way-points is distinct enough I can see two cases: The nearest way-point is either the next point or was the previous point:
So if you not only calculate the distance to the nearest point but also to the previous and next to it, you should be able to simply decide which one the next is.
As written, this requires that the resolution between the points is good enough. E.g. if you have a course with a 180 degree curve things don't evaluate that well any longer:
The solution is to have enough way-points in these areas then. So this might or might not be suitable for your problem. Sorry for the trashy graphics, but I hope they illustrate this well enough. Also the concept is a bit rough, but probably good enough to create a first mock.

How to quickly determine if multiple places are within users vicinity - Google Places API

I am designing a web app where I need to determine which places listed in my DB are in the users driving distance.
Here is a broad overview of the process that I am currently using -
Get users current location via Google's map api
Run through each place in my database(approx 100) checking if the place is within the users driving distance using the google places api. I return and parse the JSON file with PHP to see if any locations exist given the users coordinates.
If place is in users driving distance display top locations(limited to 20 by google places), other wise don't display
This process works fine when I am running through a handful of places, but running through 100 places is much slower, and makes 100 api calls. With Google's current limit of 100,000 calls per day, this could become an issue down the road.
So is there a better way to determine which places in my database are within a users driving distance? I do not want to keep track of addresses in my DB, I want to rely on Google for that.
Thanks.
You can use the formula found here to calculate the distance between zip codes:
http://support.sas.com/kb/5/325.html
This is not precise (door-step to door-step) but you can calculate the distance from the user's zip code to the location's zip code.
Using this method, you won't even have to hit Google's API.
I have an unconventional idea for you. This will be very, very odd when you think about it for the first time, as it does exactly the opposite order of what you will expect to do. However, you might get to see the logic.
In order to put it in action, you'll need a broad category of stuff that you want the user to see. For instance, I'm going to go with "supermarkets".
There is a wonderful API as part of google places called nearbySearch. Its true wonder is to allow you to rank places by distance. We will make use of this.
Pre-requisites
Modify your database and store the unique ID returned on nearbySearch places. This isn't against the ToS, and we'll need this
Get a list of those IDs.
The plan
When you get the user's location, query nearbySearch for your category, and loop through results with the following constraints:
If the result's ID matches something in your database, you have that result. Bonus #1: it's sorted by distance ascending! Bonus #2: you already get the lat-loc for it!
If the result's ID does not match, you can either silently skip it or use it and add it to your database. This means that you can quite literally update your database on-the-fly with little to no manual work as an added bonus.
When you have run through the request, you will have IDs that never came up in the results. Calculate the point-to-point distance of the furthest result in Google's data and you will have the max distance from your point. If this is too small, use the technique I described here to do a compounded search.
The only requirement is: you need to know roughly what you are searching for. However, consider this: your normal query cycle takes you anywhere between 1 and 100 google Queries. My method takes 1 for a 50km radius. :-)
To calculate distances, you will need Haversine's formula rather than doing a zip code lookup, by the way. This has the added advantage of being truly international.
Important caveats
This search method directly depends on the trade-off between the places you know about and the distance. If you are looking for less than 10km radii, use this method to only generate one request.
If, however, you have to do compounded searching, bear in mind that each request cycle will cost you 3N, where N is the number of queries generated on the last cycle. Therefore, if you only have 3 places in a 100km radius, it makes more sense to look up each place individually.

Categories