I am splitting a selected multiline shape on a click point. The second line of the split ends up with a duplicate point at the end.
I have proj4 code as I have a GoogleMapOverlay and my shapefile in the same map.
MultilineShape lineshape2 = (MultilineShape)(selectedFeaturesR[d].GetShape());
Collection<LineShape> lines = lineshape2.Lines;
LineShape lineshape = lines[0];
Collection<Vertex>pointss = lineshape.Vertices;
PointShape startpoint = new PointShape(pointss[0].X, pointss[0].Y);
lineshape = lines[lines.Count - 1];
pointss = lineshape.Vertices;
PointShape endpoint = new PointShape(pointss[pointss.Count - 1].X, pointss[pointss.Count - 1].Y);
startdistance = worldPoint.GetDistanceTo(startpoint, winformsMap1.MapUnit, DistanceUnit.Meter);
PointShape newpoint = lineshape2.GetPointOnALine(StartingPoint.FirstPoint, startdistance,winformsMap1.MapUnit, DistanceUnit.Meter);
if (newpoint != null)
{
Vertex projVertexA = proj4.ConvertToInternalProjection(newpoint.X, newpoint.Y);
int totverts = 0; distance = 999999999.99999F; tmppos = -1; mpos = -1;
for (int p = 0; p < lines.Count; p++)
{
totverts = totverts + lines[p].Vertices.Count;
for (int h = 0; h < lines[p].Vertices.Count; h++)
{
double tmpdist = newpoint.GetDistanceTo(new PointShape(lines[p].Vertices[h].X, lines[p].Vertices[h].Y), winformsMap1.MapUnit, DistanceUnit.Meter);
if (tmpdist < distance) { distance = tmpdist; mpos = p; tmppos = h; }
}
}
if (distance < tolerance) { newpoint = new PointShape(lines[d].Vertices[tmppos].X, lines[mpos].Vertices[tmppos].Y); }
projVertexA = proj4.ConvertToInternalProjection(newpoint.X, newpoint.Y);
nextid++;
values["UNIQ"] = "DTI" + nextid.ToString();
MultilineShape newshape1 = (MultilineShape)lineshape2.GetLineOnALine(startpoint, newpoint);
int vertcount1 = 0; lines = newshape1.Lines;
for (int p = 0; p < lines.Count; p++) { vertcount1 = vertcount1 + lines[p].Vertices.Count; }
roadsLayer.EditTools.BeginTransaction();
roadsLayer.EditTools.Add(newshape1, values);
roadsLayer.EditTools.CommitTransaction();
nextid++;
MultilineShape newshape2 = (MultilineShape)lineshape2.GetLineOnALine(newpoint,endpoint);
int vertcount2 = 0; lines = newshape2.Lines;
for (int p = 0; p < lines.Count; p++) { vertcount2 = vertcount2 + lines[p].Vertices.Count; }
values["UNIQ"] = "DTI" + nextid.ToString();
roadsLayer.EditTools.BeginTransaction();
roadsLayer.EditTools.Add(newshape2, values);
roadsLayer.EditTools.CommitTransaction();
roadsLayer.EditTools.BeginTransaction();
roadsLayer.EditTools.Delete(selectedFeaturesR[d].Id);
roadsLayer.EditTools.CommitTransaction();
roadsLayer.Close();
ShapeFileFeatureLayer.Rebuild(shapedir + "\\" + roadfile);
roadsLayer.Open();
}
I have had to add the following to remove the extra vertice.
int vertcount1 = 0; lines = newshape1.Lines;
for (int p = 0; p < lines.Count; p++)
{
Vertex vert1 = lines[p].Vertices[0];
for (int h = 1; h < lines[p].Vertices.Count; h++)
{
if ((vert1.X == lines[p].Vertices[h].X) && (vert1.Y == lines[p].Vertices[h].Y))
{
lines[p].Vertices.Remove(vert1);
break;
}
vert1 = lines[p].Vertices[h];
}
}
Is this an bug in the GetLineOnALine or am I doing something wrong in code.
Elisa