Hi all
I’m created a line with 7 points.
Then I shall use these coordinates to make another line, but where each linesegment can’t bo longer than 5 units. If a linesegment is longer than 5, a new point should be added at the point.
I’m having trouble to calculate where these points should be located (well, not by hand, but when writing a code that does it).
These points should then be added together with the original coordinates in a new coordinate array to create the new line.
Would really appreciate comments and tips on how to solve this.
Code so far:
[code]
import com.vividsolutions.jts.geom.*;
public class NewProj {
public static void main(String[] args){
// Create the original linestring
Coordinate[] coordinatesToLine = new Coordinate[] {
new Coordinate(12, 2),
new Coordinate(12, 13),
new Coordinate(12, 19),
new Coordinate(19, 19),
new Coordinate(20, 20),
new Coordinate(25, 25),
new Coordinate(27, 18)
};
LineString FirstLine = new GeometryFactory().createLineString(coordinatesToLine);
Point pointOne, pointTwo;
double dist;
int newLineLength = FirstLine.getNumPoints();//that number will be changed later
double newCoorX, newCoorY;
Coordinate newCoor;// ‘newCoor = new Coordinate(newCoorX, newCoorY);’ should be used in the second loop
int points2Add;
// Loop that parses the line and reads the number of points available.
// it will output an integer values that gives the length of the
// new line by calculating the distance between each two points.
for (int counter=1; counter <= FirstLine.getNumPoints()-1;counter++){
pointOne = FirstLine.getPointN(counter-1);
pointTwo = FirstLine.getPointN(counter);
dist = pointOne.distance(pointTwo);
if ( dist > 5 ){
newLineLength = newLineLength + (int)(dist / 5);//here new number of points are added
}
}
// Here is the section where the program should calculate the new points…
// I think this can be done by a loop similar to the one above. then I shall put the coordinates
// in the ‘newCoordinates’-array.
// I know that this section probably will need to be re-written completely. It’s just a try…
for (int counter=1; counter <= newLineLength;counter++){
pointOne = FirstLine.getPointN(counter-1);
pointTwo = FirstLine.getPointN(counter);
Coordinate[] newCoordinates = new Coordinate[newLineLength];
pointOne.getX();pointOne.getY();
pointTwo.getX();pointTwo.getY();
Geometry SecondLine = new GeometryFactory().createLineString(newCoordinates);
if ( pointOne.getX() – pointTwo.getX() > 5 ){
newCoorX = pointOne.getX() +5;
}
if ( pointOne.getY() – pointTwo.getY() > 5 ){
newCoorY = pointOne.getY() +5;
}
System.out.println(“Coordinates of SecondLine: ” + SecondLine.toString());
}
}
}