Lets say I have a database of locations (countries, regions, cities, towns) with their lat/long co-ordinates.
E.g. I have the co-ordinates for England 52.16045, -0.70312
Is there a way for me to return all locations within the bounds of England if all I have are lat/long?
Do I need to polygonise the location...if so how would I do that if all I have are lat/longs.
For the record the database is Mysql.
Some guidance would be appreciated.
If you have a polygon with the boundary of the state you could use a ST_CONTAINS feature of mysql geometry for find al the points inside the polygon
Assuming you have a table (points) the contain the points and polygons with polygon, and each polygon is based on a polygon.name
you could use
SELECT points.col1
FROM polygons
INNER points ON ST_CONTAINS(polygons.geom, Point(points.longitude, points.latitude))
AND polygons.name = 'Your_name';
the ST_CONTAING just check if a geometry is contained inside one other
Related
I have a bunch of locations (points) with coordinates stored as geometry points. I've imported spatial data of provinces in my country and I'm trying to determine the province in which each location lays, which worked for 26 of 28 provinces in total.
Before importing the data I noticed that all provinces had their geometries defined as POLYGON except the two in question that were defined as MULTIPOLYGON, so for consistency's sake I converted all to MULTIPOLYGON where the majority contain data for just 1 polygon.
I am testing now with one province which contains 3 polygon geometries in its MULTIPOLYGON definition. The point I'm testing with is contained within the third polygon, I have confirmed this by testing all 3 manually using the function found in this question:
Here's my SQL which returns 0 (on pastebin because SO won't let me post it here)
https://pastebin.com/raw/bkNeBgcL
If I remove the first two polygons, then it returns 1
https://pastebin.com/raw/UfTax1K5
If I'd had to guess what's going wrong is that mysql is expecting the point to be in all 3 polygons at the same time? And I need to tell it to return true if it is in any of the polygons inside the multi-polygon, but how?
I've got a list of voting precinct geospatial polygons that need to join to a different table with just lat/long points for each zip code. How can I join each of the precinct polygons to its nearest zip code lat/long point?
This article has helped me to build this script which quickly maps each of my points to the coordinate polygon it fits inside of:
import pandas as pd
import geopandas as gpd
df_zip = pd.read_csv('zipdata.csv')
gdf_pts = gpd.GeoDataFrame(df_zip, geometry=gpd.points_from_xy(df_zip.Longitude, df_zip.Latitude))
precinct_file = 'precinctdata.geojson'
gdf_coord = gpd.read_file(precinct_file)
sjoined_listings = gpd.sjoin(gdf_pts, gdf_coord, op=”within”)
# where zipdata.csv includes a latitude and longitude column along w/ other zip code data
# and precinctdata.geojson is a geojson file that includes polygons for over 100k voting precincts
Now instead of mapping each zip point to the precinct polygon it belongs to, I want to map each polygon to its nearest point so that ALL precinct polygons will have a corresponding zip code point that is the nearest point to it. Many points will map up to multiple polygons as there's over 140k precincts and less than 42k zip codes in the dataset.
I've found some similar questions here and here but weren't able to fit it into my script -- I'm new to GIS but fixed on getting going with it.
I'm trying to generate some annotations on a map. Currently, I'm using openstreetmap but the query is general. I have two end points on a map and a corpus of few selected points I would like to highlight.
The two endpoints are given in the form lat, long
<walkSteps>
<distance>9.742464221826811</distance>
<streetName>5th St NW</streetName>
<absoluteDirection>EAST</absoluteDirection>
<stayOn>false</stayOn>
<bogusName>false</bogusName>
<lon>-84.3937361115149</lon>
<lat>33.77692965678444</lat>
<elevation/>
</walkSteps>
<walkSteps>
<distance>508.2608917548245</distance>
<relativeDirection>LEFT</relativeDirection>
<streetName>Fowler St NW</streetName>
<absoluteDirection>NORTH</absoluteDirection>
<stayOn>false</stayOn>
<bogusName>false</bogusName>
<lon>-84.39363494600667</lon>
<lat>33.77692904176358</lat>
<elevation/>
</walkSteps>
My aim is to highlight those points on the map, which are present in the corpus and lie in the line connecting these two points.
How can I go about querying the corpus for the same? Annotating on map given lat, lng is not an issue
Rounding errors will prevent you from directly doing as you want. What you should be doing instead is determining the great-circle path between the two end points and highlighting those members of the corpus which are within a certain distance of the great circle route. This is known as the cross-track distance or cross-track error. Formulas for computing the cross-track distance can be found at one of the standard reference sites for geospatial equations but there are others as well.. The problem then becomes one of searching for points in the corpus which are close enough to the great circle path between the two end points.
I would like to find all points that are within N miles of a given area.
E.g. the area is California: Find all points that are within 50 miles of the border of California (not the middle of California).
When using Google Maps the distance is calculated using 'the middle' of the given location, but I need to calculate the distance using the borders of the given location. The location could be any zip code, city or country.
Could that be done by drawing a polygon using California's coordinates on a map and calculate the distance to location B using the points of the polygon?
Is there a more elegant solution to this? Any ideas?
Thanks!
I'm not sure if I understand your requirements completely, but I will give it a try with different interpretations:
1. You want to filter own map points:
This can be done with any GIS or a own service that offers a call like my_points_in_area(bbox). Bbox means here boundingbox and is the 2x lat/lon pair describing the rectangle around your given centerpoint. If you want to be accurate and really just deliver whats within 100km, you might need to test the distance to the POIs once more, as the rectangle will also include points that are a bit more far away.
2. You want to filter OSM data:
You might use a reverse-geocoding service as Nominatim to get informations about points of interests that are within this distance: http://wiki.openstreetmap.org/wiki/Nominatim
Otherwise import the OSM data using osmosis to a PostGIS DB. AFAIK there is (currently) no DB tool for Oracle: http://wiki.openstreetmap.org/wiki/Oracle
I'm sorry if I missed your question, but then please add more details :)
I have a MySQL table with cities, and for each city I have the geo coordinates. I want to build a query that determines the nearest city given coordinates of any random position. Can anyone give me an example?
Maybe I misunderstood the question, but if you have:
[X1,Y1] - the coordinates of your position
[Xn,Yn] - for each city
Then why not just calculate the distance using the simple sqrt((X1-Xn)^2 + (Y1-Yn)^2) formula?
You could optimize it further be making some clever selects, to only get the vicinity of the position from the DB and then run the distance measuring on these cities.
http://www.davidus.sk/web/main/index/article_id/8
There you go, distance is actually a radius. It will return all cities within it.