# Thread: Java Topology suit (JTS) - calculating point

1. Member Join Date
Feb 2011
Posts
9
Rep Power
0

## Java Topology suit (JTS) - calculating point

Hi all
First of; sorry if I'm starting this thread in the wrong section.

I've created a line with 7 points.
Then I shall use these coordinates to make another line, but where each linesegment can't be 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.

Java 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());

}
}

}```  Reply With Quote

2. ## can't be longer than 5 units
As in, the distance between two points? You can code a version of the distance between two points formula to check the length. In the event that the length is too long, you can use some trig to find the point on the line where a new vertex needs to be added.

The intersection of a line with a circle comes to mind (where the radius is 5 units, and the line has an angle from the origin to some destination point beyond the circle).  Reply With Quote

3. Member Join Date
Feb 2011
Posts
9
Rep Power
0

## As in, the distance between two points?
Yes, the distance between each (two) points can't be longer than five units.

The intersection of a line with a circle comes to mind (where the radius is 5 units, and the line has an angle from the origin to some destination point beyond the circle).
Any tips on how to implement this - in code?

Thank's for your reply.  Reply With Quote

4. ## Any tips on how to implement this - in code?
Well, before we get to that, do you know how to do it on paper?  Reply With Quote

5. Member Join Date
Feb 2011
Posts
9
Rep Power
0

## Solved it. Thanks for your time though.  Reply With Quote

#### Tags for this Thread

coordinate, jts, point #### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•