We are getting GPS readings from devices (laptops, phones, gps devices) and we are trying to snap them to the nearest road, so even if they are getting a reading a few meters off the road, it will still show the vehicle on the road. Is this possible?
VNH coordinate and snapping to nearest road
Jake,
This is possible and I am whipping up a code community project to show this. I should have it completed this afternoon.
David
Jake,
I have the code community project finished but it will not be posted until tomorrow due to some technical stuff. What I have done though is to pull out the function that does work and post it in this message. The code community project can show it in action but you will need to wait until tomorrow. Let me know if you have any questions.
David
private PointShape SnapToFeature(IEnumerable<FeatureLayer> snapToLayers, PointShape pointToSnap, GeographyUnit unitOfSnapToPoint, double maxDistance, DistanceUnit unitOfMaxDistance)
{
PointShape closestPoint = null;
double closestDistance = double.MaxValue;
// Loop through each layer to see which on have the closest point
foreach (FeatureLayer featureLayer in snapToLayers)
{
// Make sure the layer is open
featureLayer.Open();
// Get all the features within our tolerance
Collection<Feature> closeStreets = featureLayer.QueryTools.GetFeaturesInsideBoundingBox(new EllipseShape(pointToSnap, maxDistance, unitOfSnapToPoint, unitOfMaxDistance).GetBoundingBox(), ReturningColumnsType.NoColumns);
// Loop through each feature in our tolerance
foreach (Feature feature in closeStreets)
{
// Find out the distance to the closest point on the feature
double snapDistance = pointToSnap.GetDistanceTo(feature.GetShape(), unitOfSnapToPoint, unitOfMaxDistance);
// If the distance is closer than the previous closest distance
// then this is the new winner so far
if (snapDistance < closestDistance)
{
// Make sure the distance is within our tolerance
if (snapDistance < maxDistance)
{
closestDistance = snapDistance;
closestPoint = feature.GetShape().GetClosestPointTo(pointToSnap, unitOfSnapToPoint);
}
}
}
}
// return the closest point on the closes feature. Note that this may return null
// if there are no features within the tolerance
return closestPoint;
}
Jake,
We were able to get the code community project posted. Below is the URL.
code.thinkgeo.com/projects/show/snapgpstostreet
David