问题描述
我有一个由lat和lon给出的点,我想通过最小的欧几里得距离找到到该点的最近边缘。例如
// all imports here
const useStyles = makeStyles({
[...]
});
export default function OutlinedCard() {
const [isEditable,setIsEditable] = useState(false);
const [name,setName] = useState("");
const classes = useStyles();
const handleEdit = () => {
console.log("I am here");
setIsEditable(!isEditable);
};
return (
<Card className={classes.root} variant="outlined">
<CardContent>
<Typography
className={classes.title}
color="textSecondary"
gutterBottom
>
Word of the Day
</Typography>
<TextField
id="standard-basic"
label="Name:"
disabled={!isEditable}
value={name}
onChange={(e) => setName(e.target.value)}
onBlur={() => setIsEditable(false)}
/>
</CardContent>
<CardActions>
<Button size="small" onClick={handleEdit}>
Edit
</Button>
</CardActions>
</Card>
);
}
我明白了
import osmnx as ox
track = [(40.7052,-74.0069)]
fig,ax = ox.plot_graph(G,show=False,close=False)
for pairs in track:
ax.scatter(pairs[1],pairs[0],c='red')
plt.show()
ox.distance.get_nearest_edge(G,track,return_geom=True,return_dist=True)
它输出边缘的顶点及其几何形状。点与最近的边缘之间的距离为162。但是如何找到我的点在该最近的边缘上的投影?
解决方法
这是一个完整的最小工作示例:
import osmnx as ox
from shapely.geometry import Point
ox.config(use_cache=True,log_console=True)
# create point tuple as (lat,lng)
point = (40.7052,-74.0069)
G = ox.graph_from_point(point,network_type='drive')
u,v,k,edge_geom,dist = ox.distance.get_nearest_edge(G,point,return_geom=True,return_dist=True)
# create shapely point geometry object as (x,y),that is (lng,lat)
point_geom = Point(reversed(point))
# use shapely to find the point along the edge that is closest to the reference point
nearest_point_on_edge = edge_geom.interpolate(edge_geom.project(point_geom))
nearest_point_on_edge.coords[0]