Im using QGIS with a layer containing different routes to calculate the shortest path from point A to point B. This algorithm returns the shortest path graphically (new layer) overlapped to the original route, the cost (time) and point A and B coordinates. I want to know if there is a way to extract the data from the initial layer of the routes that the algorithms selected. For example if the original layer routes have data of state, road length in miles, etc. I want to extract this data with the shortest path.
Related
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 have 2 shapefiles that represent roads, let's call them shapes A and B. Each road is represented as line segments. File B is almost a superset of the other, with just a few roads of A not represented. File A with one segment selected (in red):
In this superset file (B), the segments are smaller. I can say that for every segment in A there are one or more segments in B. I believe there isn't a segment in B that corresponds to more than one segment in A. Here is shapefile B with one segment selected (in red):
The line coordinates aren't exact, just very near each other. Here are the coordinates of the leftmost dot of the selected line:
Dot in file A: -42.92896076999995 , -22.77139965999993
Dot in file B: -43.217942900516830, -22.888565009926047
I'm using geopandas.
How would I cross-reference the two datasets? For each line segment in file B find the associated segment in file A (if it exists)?
The question seems to depend on what you are using as a standard for cross-references. For example, you must first decide whether to assume a case where each segmentation intersects or whether to define it as the minimum distance between each segmentation.
Anyway, using geopandas or shapely for both is not a difficult task. After dividing all the segmentation of A into individual linestrings, you can use the overlay function of geopandas to find the occurrence of even a slight intersection with the segmentation of B.
You will have to decide whether to find the shortest distance orthogonal or the shortest distance between the start point and end point of each segmentation. You can use from shapely.ops import nearest_points etc. You can use all the features of shapely to target the geometry of geopandas.
I am trying to route though a set of pedestrian crossings. I ask a route to the Google Maps service to get a default route, the problem with that route is that it does not include some waypoints (pedestrian crossings) (and re-requesting a route with found walkways as waypoints does not lead to satisfying results). I have the geocoordinates of each pedestrian crossing in the streets the route is requested (for each one, I have both endpoints coordinates).
To solve the problem, I approximate the lat,lon coordinates to (x,y) values which is correct for the accuracy I need. So a route is defined by a with a set of (x,y) coordinates.
I can decompose the problem in two sub-problems:
1) Finding which pedestrian crossings should be used to reach the destination
2) Update the existing route with the found pedestrian crossings
For 1), I defined some conditions about when to attach a crossing to a given route segment.
a. Each extremity of the pedestrian crossing should be max 10m far from the current segment
b. Each extremity is located “inside" the current segment (dot product of some extremity to segment begin and to segment end should be negative)
c. I only attach one crossing to a given route segment (others are rejected) It gives me a quite correct subset of pedestrian crossings but I am not sure if it is optimal ?
For 2) I have no clue about how to efficiently merge the found pedestrian walkways with the existing route (I started looking at the A* algorithm) ?
Here is some example of route from point A to point B (in orange the pedestrian crossings, in black the input route, in blue the expected output route).
Is there a simple way to calculate the closest distance between a route (directions between two cities for example) and a GPS coordinate using the Google Maps API?
One appoach I can think of is to translate the route into a set of GPS coordinates and calculate the distance to each coordinate. Is there a better way of doing this?
One appoach I can think of is to translate the route into a set of GPS
coordinates and calculate the distance to each coordinate. Is there a
better way of doing this?
Yes there is a much better way.
The first part is correct: translate to a polyline of lat/lon coordinates.
However the second is to simple. You want the shortest distance to the line segment not the the next corner point, that is the start or end of the line.
In school you used the hessian normal distance. However this formula calculates just the distance to an infinite line. In case of a polyline you have an sequence of line segments.
So you need a formula for "distance to line segment". This one you can find using that search terms.
Having this formula implemented, you interate over all line segments each defined by two points (polyPoint[i], polyPoint[i+1]) and take the minimum of the distance.
Don't forget to transform the current line segement to cartesian (x,y) space, because the usualy formulas, do not work on spehrical coordinates.
This approach is more complex, but gives exact results. Remember a line on a route if often 400-700 meters long. So the simple approach you asked, gives in this case an error of 200-350m if you are in the middle between the two points.
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.