I worked in a project something similar of this, I used ArcObjects. My goal was connecting two adjacent polylines if one of it's end point is another one's starting point to make two short polylines into a single polyline. My process was:
1. Dictionary<PointKey, FeatureDataList> polylineDictionary;
- PointKey is a class that contains a point.
- FeatureDataList is a class that contains List of IFeatures.
Both classes overrides "Equals" and "GetHashCode" methods.
Dictionary<PointKey, FeatureDataList> ToPointDictionary;
Dictionary<PointKey, FeatureDataList> FromPointDictionary;
public void CreateDictionary(IFeatureLayer featureLayer)
{
var featureFunctionality = new FeatureFunctionality();
List<IFeature> features = GetAllFeatures(featureLayer.FeatureClass);
foreach (var feature in features)
{
IPolyline polyline = GetPolylineFromFeature(feature);
AddFeatureInDictionary(ToPointDictionary, feature, polyline.ToPoint);
AddFeatureInDictionary(FromPointDictionary, feature, polyline.FromPoint);
}
}
void AddFeatureInDictionary(Dictionary<PointKey, FeatureDataList> polylineDictionary, IFeature feature, IPoint point)
{
FeatureDataList featureDataList;
PointKey key = PointKey.GetKey(point);
if (!polylineDictionary.ContainsKey(key))
{
featureDataList = new FeatureDataList();
featureDataList.Add(feature);
polylineDictionary.Add(key, featureDataList);
}
else
{
featureDataList = polylineDictionary[key];
featureDataList.Add(feature);
}
}
By these processes I made two dictionaries. After creating dictionaries, I check for if both dictionaries contain same point and in both dictionaries, that key has only one feature in feature list, then I created a new polyline with those two polylines and delete two short polylines.
To join two polylines into one:
private IPolyline GetJoinedPolylineFromFeatures(List<IFeature> features)
{
IPolyline newPolyline = null;
if (features.Count == 2)
{
IPolyline polyline1 = feature1.Shape as IPolyline;
IPolyline polyline2 = feature2.Shape as IPolyline;
if (PointKey.GetKey(polyline1.ToPoint).Equals(PointKey.GetKey(polyline2.FromPoint)))
{
var topoOperator2 = polyline1 as ITopologicalOperator2;
if (topoOperator2 != null)
newPolyline = topoOperator2.Union(polyline2) as IPolyline;
}
else if (PointKey.GetKey(polyline1.FromPoint).Equals(PointKey.GetKey(polyline2.ToPoint)))
{
var topoOperator2 = polyline2 as ITopologicalOperator2;
if (topoOperator2 != null)
newPolyline = topoOperator2.Union(polyline1) as IPolyline;
}
}
return newPolyline;
}