Search database for locations within polyline coordinates - php

I am using Google Maps to display locations stored in a MySQL database containing name, latitude and longitude information. An AJAX request is sent and the map is updated with all the locations. So far, so good.
Users have the ability to filter the locations by drawing polylines on the map to create a bounding box. The idea is that only locations within this bounding box are returned and they can draw whatever shape they want.
All the points used to draw the shape are returned a list of coordinates. Where I am struggling is taking the list of coordinates and finding everything within them. Can I achieve this with an SQL query? Is it even possible?
I have seen examples of returning locations within x radius from one set of coordinates and also a square box, but I have no idea how to take that further and get everything within a potentially infinite set of coordinates.

You should transform the shape the user enters into a polygon. Then you transform all locations within a square around this polygon into the same coordinate system.
Using a solution describe in the wikipedia page for point-in-polygon problems you will be able to find all locations within this shape and remap them to coordinates in your sql database and then into locations.
There are however similar question which might help you find a proper solution to your problem. See:
How can I determine whether a 2D Point is within a Polygon?
Point in Polygon Algorithm

Related

Create a Polygon around lines using GPS locations in PHP

Using PHP, we have an array of GPS coordinates that create a specific route.
I need to allow the user to give an input width. Using this input we need to create a polygon around the line.
In the image found under the link below:
https://drive.google.com/file/d/1DmVdKAh6advlcQYiDZQPsKYisjxmPu5I/view?usp=sharing
We are given the latitude and longitude of the points A,B,C. I need a way to get the coordinates of the points D,E,F,G,H. Note that these are GPS locations and not 2d formulas

List points within a certain radius of a location?

I'm making an API for an iPhone app. Put simply, I have locations stored in a database (Lat/long). When a user send a request for points within, let's say, a five (5) mile radius, I'll need to make use of an algorithm to only give me those points from a MySQL database.
How can I convert miles to minutes in a coordinate? IE, with the tapering nature of longitude in mind, how much do I add to coordinates I'm searching through to compare to the users location?
2 What's a good formula to take into account the way longitude/latitude changes as you go to different points of Earth?
Thanks :)
You never mentioned how you stored the data, so I can't give you the actual code. Here's just something I came up with:
Import all the data into some efficient database. Optimize it to be efficient at pulling up coordinates within a certain X/Y range
Select all the coordinates within a 10x10 mile square. Import them into an array
Use a foreach to go through that array and use the distance formula (distance=sqrt((x-x)^2+(y-y)^2)). If within 5 miles, add it to another array. If not, ignore it.
I don't know how efficient this would be with thousands of coordinates in a small location; you might have to fine tune it (for example, try a smaller box first if you're finding the closest location).
I do have to agree with Darren that Google Maps API might be better suited, although I haven't used it before.

Google Maps API area search

I have a set of adresses / coordinates. In my software the user should be able to search for a specific adress or coordinate and find the surrounding places of the set of coordinates in a specific radius. The results should be shown on a map.
(Mostly like Google Places for Restaurants just with my own set of places)
Displaying the places on a static map is not the problem, but handling the set of addresses (~5000) and calculating the distances to the place searched for.
How can I establish a function like this using Google's APIs?
Thanks!
You can use the harvesine formula and a bounding box. Or you can use a spatial index and a bounding box. The harvesine formula is used for spheres. Most likely you use straight distance (like the crow flies).

How to detect interior vertices in groups of 2d polygons? (E.g. ZIP Codes to determine a territory)

I am looking for a method, preferably in PHP, to analyze a group of polygons to detect the outer boundaries of the group.
Specifically, this is for a Google Maps v3 application that renders territories. Each polygon in the territory is a ZIP Code. I'm trying to detect and draw only the territory boundary. Here's a mock-up of what I'm trying to accomplish:
The challenges I face in solving this problem:
ZIP Codes within each territory can be (and often are) non-contiguous (see the red and green territories in the example above).
ZIP Codes are not necessarily convex, so a convex hull technique wouldn't work (maybe I'm wrong?)
Although it looks like it in the image above, vertices are rarely truly redundant from one ZIP to another. Each lat/lon coordinate (i.e. each vertex of the polygon) has 10 decimal points of precision. I have already tried and rejected a rounding technique as it never produced a clean data set that still resembled the original shape.
On the positive side, these territories never change once they're established. Therefore, this process can be run offline to calculate and store the resulting territory polysets.
Clarification:
My data is stored at the ZIP Code level. Each ZIP Code is defined by one or more large sets of lat/lon coordinates. Each lat/lon coordinate defines a single vertex in the Google Maps polygon. As with the bigger territories, each ZIP Code may or may not be convex, and it may or may not be a single contiguous polygon. The larger territory is simply stored as a list of ZIP Codes; no polygonal data is stored for territories--which is the problem I'm trying to solve here.
Many thanks in advance for any pointers in the right direction.
It sounds like you have a list of which zip codes relate to which territories. You also have a polygon for each zip code.
If this is the case, then the solution is fairly straightforward. The key observation/assumption is that bordering zip codes should share edges, which need to be removed. If this isn't the case, then the following is a moot point.
For each territory:
Create a dictionary that is keyed to the sorted edge, and which it's item is a list of polygons with that edge. Then go through the polygons in the territory and fill out the dictionary. This may be tricky as you need to ensure that Edge(A,B) is the same as Edge(B,A), which could be done by sorting the points in the edge.
Once you've gone through all of the polygons, you'll end up with essentially a table of edges and which polygon they are associated with. Convert this table into a graph, ignoring all edges that are in two or more polygons, although the "or more" is probably not possible. The result should be an undirected cyclic graph, that you should be able to extract the territory polygon from the graph using an algorithm similar to depth-first search.
The performance should be O(N), where N is the total number of edges/vertices.

PHP Image Libraries

I'm working on a PHP application that needs to obtain data from a topographical map (different elevations denoted by different colors). I need to do two things:
Using this map (800x600), find a way to determine the exact pixel location of a particular city. For example, San Francisco is located at precisely 121x585.
Using the location from (1) above, I need to read the exact color at that location.
Note: The map provider does not provide location-based data, only a colored map. I suspect multiple libraries would be needed to map coordinates to locations on the map (via a ratio?) and then use OCR to read the color.
Are there any PHP libraries/tools that do this? How would you pull this off?
Once you know the pixel-coordinates, you can use PHP's built-in GD library to sample the color of an arbitrary pixel quite easily.
The tricky bit will be determining the pixel to sample, which can get pretty darned tricky. The earth being sphere-like, maps use various projections to produce a 2-d representation. If you know how the colored map image is projected, and you know the latitude/longitude of the pixel at (0,0), you should be able to write a function to convert lat/long to a pixel coordinate.
I may not have understood the problem entirely, but you should map the locations you want into an array (of objects?)
$city_mapping = array(new City("San Francisco", 121, 585), new City....); //Map your cities to an array.
Where City should be defined as a class to contain those variables.
Then use imagecolorat() to check for the color.

Categories