Get polygon points mysql - php

i have created a table in mysql to store polygon data:
CREATE TABLE geom (g GEOMETRY);
And i inserted a polygon into it, using the following syntax:
INSERT INTO geom (g)
VALUES(PolygonFromText('POLYGON((
9.190586853 45.464518970,
9.190602686 45.463993916,
9.191572471 45.464001929,
9.191613325 45.463884676,
9.192136130 45.463880767,
9.192111509 45.464095594,
9.192427961 45.464117804,
9.192417811 45.464112862,
9.192509035 45.464225851,
9.192493139 45.464371079,
9.192448471 45.464439002,
9.192387444 45.464477861,
9.192051402 45.464483037,
9.192012814 45.464643592,
9.191640825 45.464647090,
9.191622331 45.464506215,
9.190586853 45.464518970))')
);
Now how can i get back the vertices (points) of this polygon in mysql? Why i am asking means, later i want to find whether a point is inside a polygon. And to achieve this, i hope i need the polygon vertices.

If you want the WKT back: SELECT AsText(g) FROM geom;

To answer your question, a great option would be to output the GeoJSON format.
See more here: (https://dev.mysql.com/doc/refman/5.7/en/spatial-geojson-functions.html)
select ST_AsGeoJSON(g) from geom;
To help with your actual problem of selecting points in the bounding polygon: #jcorry has a great solution with ST_Contains
To further expand this out to select all points from a coordinates table that the Polygon encapsulates, you can do something like:
select
ST_X(point) as x,
ST_Y(point) as y
from coordinates
where ST_Contains(
(select g from geom limit 1),
point
)

If you want to find whether a point is in a polygon, you don't need to derive the individual vertices to do that. There's a function in MySQL (5.6+) for this:
SELECT ST_Contains(PolygonFromText('POLYGON((
9.190586853 45.464518970,
9.190602686 45.463993916,
9.191572471 45.464001929,
9.191613325 45.463884676,
9.192136130 45.463880767,
9.192111509 45.464095594,
9.192427961 45.464117804,
9.192417811 45.464112862,
9.192509035 45.464225851,
9.192493139 45.464371079,
9.192448471 45.464439002,
9.192387444 45.464477861,
9.192051402 45.464483037,
9.192012814 45.464643592,
9.191640825 45.464647090,
9.191622331 45.464506215,
9.190586853 45.464518970))'), PointFromText("POINT(10 42)")
);

Related

Find the smallest number in php / SQL

I have a code that looks for the smallest number in the average column. When in mysql, I put the column in "double", it works. But for some reasons and particular actions, my column must be in "text". I need a php / SQL code to show the smallest number.otherwise my code tells me that 13.06 is smaller than 8.56.here is my code:
$sql1 = "SELECT MIN(moyenne)as moyenneP FROM rangec where classe = '".$_SESSION['classroom']."' AND position = '".$_SESSION['anscol']."'
AND ecoles= '".$_SESSION['ecoles']."' AND trimestre= '".$_SESSION['trimestre']."'";
foreach ($bdd->query($sql1) as $classement)
{
$moyenneP =$classement['moyenneP'];
$moyenneP=''.round($moyenneP, 2).'';
}
I believe I have been explicit! Thank you in advance for your assistance.

Point in polygon using geoPHP

I want to check a point exists in a polygon or not using GEOPHP in Drupal. I have created the polygon and got the coordinates from http://www.birdtheme.org/useful/v3tool.html.
$userLocation = geoPHP::load("POINT(77.600555 12.965416)","wkt");
$storeServingArea=geoPHP::load("POLYGON((77.569656 12.986159, 77.596779 12.993518, 77.622185 12.973780, 77.623215 12.954710, 77.608109 12.945676, 77.577896 12.945341, 77.565880 12.957386, 77.565193 12.976122, 77.568970 12.985824))","wkt");
var_dump($storeServingArea->contains($userLocation));
This code gives the result as NULL.
Then I tried to exchange the values of latitude and longitude and tested like this:
$userLocation = geoPHP::load("POINT(12.965416 77.600555)","wkt");
$storeServingArea=geoPHP::load("POLYGON((12.986159 77.569656, 12.993518 77.596779, 12.973780 77.622185, 12.954710 77.623215, 12.945676 77.608109, 12.945341 77.577896, 12.957386 77.565880, 12.976122 77.565193, 12.985824 77.568970))","wkt");
var_dump($storeServingArea->contains($userLocation));
This also give the result as NULL.
What could be the possible mistake I'm doing here? how can I fix it?
SOLUTION:
Helpful link: Check if Google Map Point is in polygon from PHP
I fixed my issue with the following link:
http://assemblysys.com/php-point-in-polygon-algorithm/
I have forked geoPHP and added Polygon->pointInPolygon and MultiPolygon->pointInPolygon methods for that purpose. Take a look: https://github.com/sookoll/geoPHP
$point = \geoPHP::load('POINT (x y)','wkt');
$polygon = \geoPHP::load('POLYGON ((x y...))','wkt');
$point_is_in_polygon = $polygon->pointInPolygon($point);

How to retrieve latitude and longitude point from Multipolygon in MySQL

Has anyone used PHP to parse the Polygon/Multipolygon geometry data held in MySql?
I am new to the spatial function in MySQL. Please help me take the latitude and longitude point from a multipolygon in MySQL.
I have a table which has a geometry column of type MULTIPOLYGON and I wish to list all of the discreet points in the polygon(s).
First, I obtain the Nth polygon of interest as text:
SELECT AsText( GeometryN( polygon_geom, 1)) FROM spatial_search_geometry
This gives me a string in this form:
POLYGON((280593.054673555 254679.164020664,280554.160845461
254662.495237195,280515.267017367 254645.826453727,280531.935800836 254573.595058695,280562.495237195 254448.57918268,280593.054673555 254270.77882568,280556.938976039 254065.197162898,280518.045147945 253745.712146414,280476.373189273 253759.602799305,280429.144969445 253779.049713352,280423.588708289 253876.284283586,280398.585533086 253917.956242258,280454.148144648 253981.853245555,280512.488886789 254101.312860414,280518.045147945 254231.884997586,280495.82010332 254368.013395914,280468.038797539 254423.576007477,280495.82010332 254454.135443836,280501.376364477 254490.251141352,280429.144969445 254604.154495055,280493.041972742 254643.048323148,280479.151319852 254690.276542977,280495.82010332 254795.845504945,280473.595058695 254848.62998593,280493.041972742 254948.642686742,280462.482536383 254998.649037148,280448.591883492 255087.549215648,280484.707581008 255223.677613977,280556.938976039 255426.48114618,280593.054673555 255423.703015602,280645.839154539 255420.924885023,280709.736157836 255429.259276758,280729.183071883 255387.587318086,280729.183071883 255323.690314789,280706.958027258 255276.462094961,280684.732982633 255254.237050336,280651.395415695 255232.012005711,280656.951676852 255195.896308195,280665.286068586 255126.443043742,280665.286068586 255076.436693336,280665.286068586 255029.208473508,280620.835979336 254951.42081732,280584.72028182 254934.752033852,280531.935800836 254940.308295008,280593.054673555 254679.164020664))
From this output how do I get each latitude and longitude point?
looks like you need to parse an array, how about this?
Assuming that the first number is Longitude then Latitude and each pair of numbers are the appropriate coordinates.
$i=0;
foreach(POLYGON[]){
echo="Longitude: ".POLYGON[$i];
echo="Latitude: ".POLYGON[$i+1];
$i= $i+2;
}

How to find the nearest cities using web services? [duplicate]

Do you know some utility or a web site where I can give US city,state and radial distance in miles as input and it would return me all the cities within that radius?
Thanks!
Here is how I do it.
You can obtain a list of city, st, zip codes and their latitudes and longitudes.
(I can't recall off the top of my head where we got ours)
edit: http://geonames.usgs.gov/domestic/download_data.htm
like someone mentioned above would probably work.
Then you can write a method to calculate the min and max latitude and longitudes based on a radius, and query for all cities between those min and max. Then loop through and calculate the distance and remove any that are not in the radius
double latitude1 = Double.parseDouble(zipCodes.getLatitude().toString());
double longitude1 = Double.parseDouble(zipCodes.getLongitude().toString());
//Upper reaches of possible boundaries
double upperLatBound = latitude1 + Double.parseDouble(distance)/40.0;
double lowerLatBound = latitude1 - Double.parseDouble(distance)/40.0;
double upperLongBound = longitude1 + Double.parseDouble(distance)/40.0;
double lowerLongBound = longitude1 - Double.parseDouble(distance)/40.0;
//pull back possible matches
SimpleCriteria zipCriteria = new SimpleCriteria();
zipCriteria.isBetween(ZipCodesPeer.LONGITUDE, lowerLongBound, upperLongBound);
zipCriteria.isBetween(ZipCodesPeer.LATITUDE, lowerLatBound, upperLatBound);
List zipList = ZipCodesPeer.doSelect(zipCriteria);
ArrayList acceptList = new ArrayList();
if(zipList != null)
{
for(int i = 0; i < zipList.size(); i++)
{
ZipCodes tempZip = (ZipCodes)zipList.get(i);
double tempLat = new Double(tempZip.getLatitude().toString()).doubleValue();
double tempLon = new Double(tempZip.getLongitude().toString()).doubleValue();
double d = 3963.0 * Math.acos(Math.sin(latitude1 * Math.PI/180) * Math.sin(tempLat * Math.PI/180) + Math.cos(latitude1 * Math.PI/180) * Math.cos(tempLat * Math.PI/180) * Math.cos(tempLon*Math.PI/180 -longitude1 * Math.PI/180));
if(d < Double.parseDouble(distance))
{
acceptList.add(((ZipCodes)zipList.get(i)).getZipCd());
}
}
}
There's an excerpt of my code, hopefully you can see what's happening. I start out with one ZipCodes( a table in my DB), then I pull back possible matches, and finally I weed out those who are not in the radius.
Oracle, PostGIS, mysql with GIS extensions, sqlite with GIS extensions all support this kind of queries.
If you don't have the dataset look at:
http://www.geonames.org/
Take a look at this web service advertised on xmethods.net. It requires a subscription to actually use, but claims to do what you need.
The advertised method in question's description:
GetPlacesWithin Returns a list of geo
places within a specified distance
from a given place. Parameters: place
- place name (65 char max), state - 2 letter state code (not required for
zip codes), distance - distance in
miles, placeTypeToFind - type of place
to look for: ZipCode or City
(including any villages, towns, etc).
http://xmethods.net/ve2/ViewListing.po?key=uuid:5428B3DD-C7C6-E1A8-87D6-461729AF02C0
You can obtain a pretty good database of geolocated cities/placenames from http://geonames.usgs.gov - find an appropriate database dump, import it into your DB, and performing the kind of query your need is pretty straightforward, particularly if your DBMS supports some kind of spatial queries (e.g. like Oracle Spatial, MySQL Spatial Extensions, PostGIS or SQLServer 2008)
See also: how to do location based search
I do not have a website, but we have implemented this both in Oracle as a database function and in SAS as a statistics macro. It only requires a database with all cities and their lat and long.
Maybe this can help. The project is configured in kilometers though. You can modify these in CityDAO.java
public List<City> findCityInRange(GeoPoint geoPoint, double distance) {
List<City> cities = new ArrayList<City>();
QueryBuilder queryBuilder = geoDistanceQuery("geoPoint")
.point(geoPoint.getLat(), geoPoint.getLon())
//.distance(distance, DistanceUnit.KILOMETERS) original
.distance(distance, DistanceUnit.MILES)
.optimizeBbox("memory")
.geoDistance(GeoDistance.ARC);
SearchRequestBuilder builder = esClient.getClient()
.prepareSearch(INDEX)
.setTypes("city")
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setScroll(new TimeValue(60000))
.setSize(100).setExplain(true)
.setPostFilter(queryBuilder)
.addSort(SortBuilders.geoDistanceSort("geoPoint")
.order(SortOrder.ASC)
.point(geoPoint.getLat(), geoPoint.getLon())
//.unit(DistanceUnit.KILOMETERS)); Original
.unit(DistanceUnit.MILES));
SearchResponse response = builder
.execute()
.actionGet();
SearchHit[] hits = response.getHits().getHits();
scroll:
while (true) {
for (SearchHit hit : hits) {
Map<String, Object> result = hit.getSource();
cities.add(mapper.convertValue(result, City.class));
}
response = esClient.getClient().prepareSearchScroll(response.getScrollId()).setScroll(new TimeValue(60000)).execute().actionGet();
if (response.getHits().getHits().length == 0) {
break scroll;
}
}
return cities;
}
The "LocationFinder\src\main\resources\json\cities.json" file contains all cities from Belgium. You can delete or create entries if you want too. As long as you don't change the names and/or structure, no code changes are required.
Make sure to read the README https://github.com/GlennVanSchil/LocationFinder

Load markers from database for the visible area

I'm trying to load markers (custom overlay actually) from my database which contains the lat and the lng of each markers. I would like to load only the markers in the current visible area of the google map.
What I do is that I am getting the map bounds and calculate the max and min lat and lng. Each time I drag or change the zoom, I'm sending those bounds using ajax and then I retrieve the right markers from my database using a sql query which looks like this:
AND lat BETWEEN :minLat AND :maxLat
AND lng BETWEEN :minLng AND :maxLng
That is working fine... except when I'm currently looking between the 2 repeated worlds of a google map.
The sql query will not work anymore with those map bounds (sw and ne) because each bound are from their respective worlds. Then I won't retrieve any markers.
Example : Australia is about lng = 150, and in this case I would have
minLng = 25 (sw) and maxLng = 0 (ne)... obviously 150 is not between 0 and 25 even if I'm looking at Australia right now.
I have not seen any question on that particular problem...
Any idea to solve that? What am I doing wrong?
EDIT : I finally solved my problem... here is what I did:
// normal case
if( $swLng < $neLng )
{
$ANDOR_lng = "AND";
}
// Case where world repeated
else
{
$ANDOR_lng = "OR";
}
$dql = "SELECT MYSTUFF ...
AND lat BETWEEN :latA AND :latB
AND ( lng > :lngA ".$ANDOR_lng." lng < :lngB )";
$parameters = array(
'latA' => $swLat,
'latB' => $neLat,
'lngA' => $swLng,
'lngB' => $neLng
);
}
Normally, you would expect w (West longitude) to be smaller than e (East longitude), that is:
:minLng < :maxLng
However, but when the bounding box contains the meridian at 180 (or -180) this is not true. In those cases, this part of the query can never return true:
( lng > :minLng AND lng < :maxLng )
What you could do is swap :minLng and :maxLng so that the above assumption becomes true again:
if(:minLng > :maxLng) {
:minLng, :maxLng = :maxLng, :minLng
}
If you look at your map above, that's consistent with what you're doing when drawing it: the sw corner has a greater longitude than the ne corner.
Warning: I haven't written PHP code for years, I don't know if you can actually do a, b = b, a to swap their values, I only know that works in Python, but you get the idea.
The problem is that you uses the wrong bounds. You need to get the pixel bound and not the coordinate bounds. If you get the pixel bound then I think the query can work because there isn't such problem.

Categories