Working with coordinates in CodeIgniter 4 (or PHP) - 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;

Related

Get all the users within radius from MySQL with Spatial Point

I have a MySQL table on my local server. The table includes latitude, longitude and POINT(latitude, longitude) for each user. I am trying to get all the id user within 10 kilometres from center.
I would like not to use latitude and longitude but only the Point. But I don't know how to do it. I also heard about SRID to modify in the POINT but I did not quite understand
Here is my table for the moment
# Name Type
1 lat decimal(10,8)
2 lng decimal(11,8)
3 latlng point
4 id_user int(10)
Indexes
Keyname Type Unique Packed Column Cardinality Collation Null Comment
latlng SPATIAL No No latlng (32) A No
How could I do that in PHP?
Just for information, what is the best thing between using a Point and latitude/longitude?
I am a postgres/postgis user, but they have equivalents. If you have the ability to add columns to your table you should convert the lat/lons to Point() objects maybe using something like ST_PointFromText and then you can compare them to each other using ST_Distance()
Ref: https://dev.mysql.com/doc/refman/8.0/en/gis-data-formats.html
Edit: apparently mySql does not have the more efficient ST_DWithin() (See: Whats the equivalent of ST_DWithin (PostGIS - PostgreSQL) for MySQL?) so you probably stuck with ST_Distance().
SRID 4326 is a common one for these types of lat/lon coordinate systems.
Ref: https://desktop.arcgis.com/en/arcmap/10.3/manage-data/using-sql-with-gdbs/what-is-an-srid.htm

MYSQL: Using VARCHAR field as a POINT in geospatial query

I've decided to detect distance between two points. One of the points is static -35.735019 51.410041, however, the other one is a point which is given by a database field.
Here is my code:
SELECT
r0_.restaurant_point AS restaurant_point_0,
ST_Distance(GeomFromText('POINT(35.735019 51.410041)'), GeomFromText('POINT(r0_.restaurant_point)')) *
100 AS sclr_1
FROM restaurants r0_
We stored r0.restaurant_point as a VARCHAR. Take the string below as an example: 35.73062161548168 51.410930156707764
Although it would be worked if I change r0.restaurant_point with static value, it can't get measure with database field.
Is there anyway for binding this value or another way to resolve?
To concatenate the value of the column restaurant_point in the string that represents the point, you can use CONCAT().
GeomFromText(CONCAT('POINT(', t.restaurant_point, ')'))
In your case you are just trying to create a Point NOT from the coordinates in the column, BUT from the column name itself. you need to change your code as follows:
SELECT
r0_.restaurant_point AS restaurant_point_0,
ST_Distance(
GeomFromText('POINT(35.735019 51.410041)'),
GeomFromText(CONCAT('POINT(', r0_.restaurant_point, ')'))
) * 100 AS sclr_1
FROM restaurants r0_

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

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.

How do i find a neighborhood for a lat/lon in polygon data php

I have got polygon data for some neighborhoods in New York city from maponics and imported into a MySQL database. I have a requirement to find out the corresponding neighborhood from the polygon for a given lat/lon.
Is there anyway to achieve this in PHP?
MySQL version 5.6 added ST_CONTAINS() function which will do a true spatial search if a point is within a polygon. You will need to do the following:
1) ST_CONTAINS( polygon, point ) : the first parameter should be the column name in the table containing your polygon.
2) The second parameter is a POINT datatype constructed using the spatial function GeomFromText()
3) The MySQL query below will return the row (neighborhood) which contains the lat/lng point.
SELECT * FROM your_table where ST_Contains( your_polygon_column, GeomFromText( 'POINT( your_lat, your_lon )' ) );

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

Categories