Finding Lat/Long using polygon or circle as search - php

I'm out of my element here so please forgive me if I dont ask this correctly/clearly.
I have a table of users with lat/long for each one.
I get a query from a federal agency that provides either a polygon or a circle like this:
<polygon>38.47,-120.14 38.34,-119.95 38.52,-119.74 38.62,-119.89 38.47,-120.14</polygon>
<circle>32.9525,-115.5527 0</circle>
Given either of these two inputs, I need to return all users within the polygon or circle.
I've looked all over, read all the mysql spatial docs etc. and I am either missing a step or just not grokking the query method.
ANY help would be appreciated!

Have you read http://howto-use-mysql-spatial-ext.blogspot.com/ ?

This is the answer that I came up with and appears to be working:
First, I altered the table by adding a field to hold the spatial points created from the users lat/lon:
ALTER TABLE users ADD location POINT NOT NULL;
UPDATE users set location = PointFromText(CONCAT('POINT(',lat,' ',lon,')'))
CREATE SPATIAL INDEX location ON users(location);
From there, I'm using this to query to find the users within the polygon:
SET #bbox = 'POLYGON((38.47,-120.14 38.34,-119.95 38.52,-119.74 38.62,-119.89 38.47,-120.14))';
SELECT * , AsText( location )
FROM users
WHERE Intersects( location, GeomFromText( #bbox ) ) ;
For new users I created a trigger to update the location from the lat/lon (user can't be added without a lat\lon):
DROP TRIGGER IF EXISTS `users_insert_defaults`;
DELIMITER //
CREATE TRIGGER `users_insert_defaults` BEFORE INSERT ON `users`
FOR EACH ROW SET NEW.location = PointFromText(CONCAT('POINT(',NEW.lat,' ',NEW.lon,')'))
//
DELIMITER ;

For a circle, you would need to compute the distance from the circle center to each of the lat/lon's in your persons list. If the distance is less than the radius of the circle, that person is inside the circle. Typical formula for computing distances between 2 lat/lon's along the 'great circle' is called the haversine formula. The website below shows this formula in javascript.
http://www.movable-type.co.uk/scripts/latlong.html
For a general polygon, this is much harder.
For general point in a polygon, the Point Inclusion Test seems to work assuming a trivial Equirectanglular projection (x = lon, y = lat).

Related

Retrieving records from the database and comparing those records with the current values

I've an Android App where I take the GPS position, and I have a back-end REST service written in Laravel, and in my DB I have a table named 'GPS' which saves the GPS coordinates when I click on a 'Save' button.
When I click on the 'Save' button on My app, it must check whether the current position is within a radius of X meters when compared to one of the saved GPS on my DB.
I have no idea on doing it, is it possible?
Table GPS field are:
id, gps
Depends on how your GPS column looks like but you can easily calculate the distance from two gps (lat, lng) points with the haversine formula.
See this nice explanation in Javascript: http://www.movable-type.co.uk/scripts/latlong.html
Basically, you calculate the distance between your two points and then check if the result is within your radius.
For PHP, just search stackoverflow. For calculation with MySQL, take a look at this answer.
I have a plan for it:
1. create new route
2. create controller or use exist
3. create action
4. create function for search these points in your DB
5. provide it in JSON or XML format
Just use it from point to point.

PHP Radius Search

I'm going to build an app where the users can see points of interest in a predefined radius around their location.
My first idea was to store the latitude and longitude of all POI's in a database and comparing the users location with the POI's location via SQL.
The problem is the performance I think. If there are thousands of POI's and thousands of user requests with their location, it wouldn't be very economically or is this no problem for todays servers?
My next approach was to divide the map in quadrants, and only observing the surrounding quadrants.
tl;dr:
All in all I'm looking for:
a way to do an radius search
at best caching the results for other users
the cache will be updated when a new POI is being registered.
If you have any ideas how to realize something like that, please let me know.
Thank you
Fabian
I think what you are looking for is the Harversine formula, which it allows you to find the distance between two points in a sphere (in this case the Earth). An implementation using SQL would be something like this:
ACOS (
SIN(RADIANS($latitude)) *
SIN(RADIANS(T.latitude))+
COS(RADIANS($latitude)) *
COS(RADIANS(T.latitude))*
COS(RADIANS($longitude-T.longitud)))*6378.137 AS distance
Adding this to the select of your query will return a column called distance calculating (in Km) how far is the point ($latitude,$longitude), normally the user, from (T.latitude,T.longitude), normally the element of the table.
In case you want to filter, and don't show elements further than a certain distance you can make a condition like:
HAVING distance<$radius
I imagine that you are using MySQL, if this is the case you have to use HAVING instead of WHERE to make a condition over a computed column (distance).
A complete example of a query would be like this:
SELECT T.*, ACOS (
SIN(RADIANS($latitude)) *
SIN(RADIANS(T.latitude))+
COS(RADIANS($latitude)) *
COS(RADIANS(T.latitude))*
COS(RADIANS($longitude-T.longitud)))*6378.137 AS distance
FROM your_table as T
HAVING distance < $radius
ORDER BY distance LIMIT $limit
If you want to optimize a bit more the performance add a limit to the query so you will have for example the 10 nearest places.
Take your time to consider Spatial data types aswell since they were specificly made for this kind of work.
Note that I do not recommend you to insert your php variables directly into your query, is really unsecure, I did that only as an example.
Hope this helps you.

Determining location from Lat/Lng to match second table

I've done some reading so far and I am at a crossroads.
My situation is this:
Table with a list of lat/lng values ( we can take these to be "cities" ) with a radius
Table with a list of movement values, including a lat/lng
My requirement is that I return the list of movements and include the nearest city (if it's within the radius). I've so far used the haversine formula in PHP to do this for each record I return but it's not particularly efficient.
My two options I've found is either to:
1/ create a stored procedure in MySQL to do the Haversine that side (something like this: Proximity Search )
2/ use a "bounding box" method of positioning the cities instead of a circle. This is not a big problem and would allow the sql to be simplified. However, in some cases the typical logic of determining whether the point lies between the top left and bottom right will not work if the lat and lng are in negatives. In PHP, to work around this, I would do a quick "if" where I'd check if the top-left lat/lng is greater than the bottom-right and use AND/OR depending on the result.
After some extra reading I found this question here on SO.
https://stackoverflow.com/a/5548877/1749630
This answer was exactly what I needed to point me in the right direction. In this way I am now using a sub-select in order to find the city.
If someone posts a better answer than this one I'll mark that instead of this one :)

Matching long & lat within radius

I'm thinking of using my map coordinates to allow for location based searching and I don't know where to start.
I have a database of coordidinates - When I enter a suburb, I want to display all matches within a given radius. (i.e. search database of long/lat coords and return all coords that are in an x radius of long/lat of suburb searched)
I'm not sure how to achieve this or where to start?
Well it really depends on what database you are using.
the general technique is to use a scaler value function that uses a formula such as:
distance = ACOS(SIN(lat1)*SIN(lat2)+COS(lat1)*COS(lat2)*COS(lon2-lon1))*6371
then you can write a query like:
select *
from locations
where mydistancefunction(lat1,lng1,locations.lat,locations.lng) < #radius
Are you aware of mysql spatial extensions?Try to create a spatial column in your table i.e. Point and then use the functions that spatial extensions offer so you don't reinvent the wheel

Mysql retrieve polygon data

i have been developing a site that stores spatial data in mysql database, like that of buildings, gardens, etc. in the form of polygons (latitudes and longitudes).
I want to know how to retrieve polygon data in mysql.
I have seen this sample query to insert a polygon data:
http://amper.110mb.com/SPAT/mysql_initgeometry2.htm
But now i want to know how to retrieve data from the table, based on certain constraints like:
"where latitude < 9.33 and longitude > 22.4"
Also how do i find whether a point lies inside or outside of a polygon
Here is a page with lots of examples: http://howto-use-mysql-spatial-ext.blogspot.com/
This is one of the examples to retrieve rows which points intersect with a specified bounding box:
SET #bbox = 'POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))';
SELECT name, AsText(location) FROM Points
WHERE Intersects( location, GeomFromText(#bbox) );
The MySQL documentation says these spatial functions also work with geometries (Looks like a Point is also a Geometry). So you can check if the geometry in the database intersects with the one you specify in the select statement.

Categories