Laravel + Mysql: Retrieve POI's within a bounding box - php

I have a database table with POI's that i want to connect with a record in my Geo table (containing cities, countries).
Within the POI table i have a Lat & Lon field.
Within the Geo table i have 4 coordinates, for West/East and North/South
I'm using Laravel 5 / Eloquent and Mysql. The coordinates are stored as a decimal (11,8). I can add multiple columns if needed.
How can i check if a POI is located within those 4 coordinates?

Mysql supports spatial data types and functions that were designed to make such calculations easy.
Define your POIs as POINT and your rectangle as GEOMETRY. Use the ST_Contains() function to test whether the point is within the rectangle:
select * from yourtable
where st_contains(rectangle_field, point_field)
As far as I know, Laravel does not directly support these daya types and functions, so you have to resort to raw sql.

Related

Working with coordinates in CodeIgniter 4 (or PHP)

I am programming in CodeIgniter 4, and I don't know how to deal with the following problem using either CodeIgniter 4 or directly with PHP.
I have a database table places with one column coord with the position (coordinates) of each place, a MySQL column of type point. Thanks to CodeIgniter 4 I get all the columns, but in order to use the latitude and longitude separately I need to do:
select (*, st_x(coord) as lat, st_y(coord) as lon)
from ...
That way, I can use lat and lon. If I just use coord I get something without meaning.
Is there a way to do one of the following:
Work with that coord variable to get the latitude and longitude in PHP?
Add a function in the MySQL table to make that select(*) return also the lat and lon columns (calculated from st_x(coord) and st_y(coord))?
You could add virtual generated columns for lat and lon -
ALTER TABLE places
ADD COLUMN lat DOUBLE GENERATED ALWAYS AS (ST_X(coord)) VIRTUAL,
ADD COLUMN lon DOUBLE GENERATED ALWAYS AS (ST_Y(coord)) VIRTUAL;

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.

Include distance in search form

I would like to implement a search by distance on a website.
There must be a user in a living city can find all users living within 100 or 200 km for example.
I have a table in my database that stores all the cities and their coordinates.
I thought to create another table that would store the distance between all cities but my data base contains 36,000 cities and it may make a lot of records ...
How could I make this search more simply knowing that my project will be developed with Symfony and Doctrine?
Thank you beforehand
You can use the correct answer here to determine the distance between co-ordinates.
Measuring the distance between two coordinates in PHP
For performance reasons you need to use geospatial index to efficiently query such a database. For example MongoDB has a feature for this.
If performance is not an issue you can simply store locations in relational database table and calculate distances in SQL. See this question for some information about this solution: Geo-Search (Distance) in PHP/MySQL (Performance)

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