How to store lat/long in MySQL database - mysql

I'm working on Google map API. after i choose the start and end point the map return the routed way as a path on the google map, and i have track the lat/lng from the routed path
as follows [ the coordinates below are the lat/long of a routed line]
13.692941, 100.750723,
13.70649,100.75405999999998,
13.71334,100.75428999999997,
13.72268,100.74638000000004,
13.72775,100.74631,
13.8153,100.73532999999998,
13.81332,100.73160000000007,
i want to store all these lat/long in MySQL database.
i found that there is Spatial extension in MySQL.
and there is a way to insert lat/long as linestring
insert into geom (g)
values (GeomFromText('linestring(2 3,7 5,10 10)'))
i wanna know how to add all those above lat long into linestring function?
or there is another suggestion?

You can store these lat/lon DATA like bellow -
$arr = array("13.692941, 100.750723",
"13.70649,100.75405999999998",
"13.71334,100.75428999999997",
"13.72268,100.74638000000004");
// serialize data before save to database, you should deserialized that when you will use this data after query.
$serializedArr = serialize($arr);
insert into geom (g) values ("{$serializedArr}");

Related

Saving MySQL spatial data Google maps API Bounds

So I would like to save lat, lang and bounds spatial data to MySQL for now only to set a view port and zoom for places search. I saved lat, lang as POINT type and everything is fine but I'm stuck with bounds. I tried saving it as POLYGON but with no luck is it even a POLYGON? Inserting with PHPmyAdmin seems broken.. And I can't find straight forward explanation anywhere..
I tried inserting like that
(ST_GeomFromText('POLYGON((0 0, 0 0))'))
And it went through but when I proper data MySQL throws warning about null values
(ST_GeomFromText('POLYGON((29.3772 60.517,38.490877 74.889862))'))
Also what's even more confusing is that rows get inserted even though column is set as NOT NULL.

Can Neo4j Spatial Geoserver find the country, city given an ip address, if so, how to set up on nodes already created?

I have some data in Neo4j with nodes having ip addresses as properties and I would like to get lat, lon (and city, country) data based on the ip address, similar to using geoip.dat. Can Neo4j Spatial give me this data? What are the steps to achieve this?
Neo4j spatial does not support finding location based on ip. You must use an API designed for getting location off of IP , like http://freegeoip.net/.. you can try calling this API directly with cypher
MATCH (m:Entity)
CALL apoc.load.json("http://freegeoip.net/json/?q=" + m.ip) YIELD value
//return response
RETURN value

SQL selecting spatial points

I am trying to select a spatial point in SQL so that I can receive the latitude and coordinate of the point in the most simple form possible.
Currently my query looks like this:
SELECT AsText(`coordinates`) FROM table
This returns something along the lines of:
POINT(53.432985 -1.357258)
Are there any other spatial functions that will allow me to return these as two seperate values or at least make them a little easier to perform something like substr on them?
Ideally I'd like it to return two values, a latitude value and a longitude value
This is the geospatial way to retrieve latitude/longitude from a POINT type, assuming that coordinates is stored as a POINT type. X will return the latitude and Y the longitude.
SELECT X(coordinates),Y(coordinates) FROM table
I assume your geometry field is called coordinates?
SELECT location.STY as Lat, location.STX as Lon from yourTableName
If it's a geography datatype try
SELECT location.Lat as Lat, location.Long as Lon from yourTablename

Select items within range using MySQL spatial extensions?

I am completely new to MySQL spatial extensions, so please excuse my ignorance on the topic.
The scenario:
1) I collect a user's location via latitude and longitude. I store that location in a MySQL table.
2) I then display any other users within a range of that user. The range would be small - let's say 1000 ft.
Question:
What is the best practice for storing the location points? What is the best practice for querying and selecting the points nearest that user?
Use the POINT Spatial datatype to store the coordinates.
The structure is like this:
CREATE TABLE gps name varchar(0), `location` point NOT NULL;
Please note the type of column 'location'.
After that, you have to define a SPATIAL index for the 'location' column.
CREATE SPATIAL INDEX sp_index ON gps(`location`);
Inserting data (latitude and longitude)
INSERT INTO gps (name, location) VALUES ( 'India' , GeomFromText( ' POINT(31.5 42.2) ' ) )
The function GeomFromText takes a string and returns a Geometry Object. Here 31.5 and 42.2 are latitude and longitude respectively. Note that there is NO comma between them. They are delimited by a space.
Reading data
SELECT name, AsText(location) FROM gps;
The AsText function converts the internal representation of a geometry to a string format.
Note: You have to use MySQL 5.0 or above the spatial extension support. Also use 'MyIsam' db engine for faster queries.
Unfortunately there is no such function in MySQL as select points in radius. But you can easily use this cosine law formula: d = acos( sin(φ1)*sin(φ2) + cos(φ1)*cos(φ2)*cos(Δλ) )*R to get distance between 2 points. You can find details here: http://www.movable-type.co.uk/scripts/latlong-db.html
SQL query will look like this:
SELECT acos(sin(radians(Y(point1)))*sin(radians(Y(point2))) + cos(radians(Y(point1)))*cos(radians(Y(point2)))*cos(radians(X(point2))-radians(X(point1)))) * 6371 AS `distance`
FROM locations

MySql find country via geometry / spatial data

i want request my db with LAT/LON an get back a countrycode.
i have the world borders in a mysql database (converted shapefile from djangoproject.com) as geometry datafield.
When using
SELECT countyname FROM `world` WHERE Contains(ogc_geom, POINT(-18, 64))
i get back not only "iceland" (which is true) but also greenland, russia and usa.
I tried other points (point in mongolia founds also in china and russia), tried using intersects(), checked with multipolygons...
good ideas?
thanks, stefan
I am not sure you can do this only using MySQL. If you can interface the MySQL data with a scripting language, the n you can try the following.
Generate an image of the world map. Each pixel in the image will store the country id. You can lookup the country details from the MySQL db using this id.