/    Sign up×
Community /Pin to ProfileBookmark

JTS – caclulation help required

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

}
}

}
[/code]

to post a comment
Java

2 Comments(s)

Copy linkTweet thisAlerts:
@KorFeb 18.2011 — It is amazing how many people, you included, do not make the difference between [B]Java [/B]and[B] JavaScript[/B]. As here is the [I]JavaScript[/I] Forum, I will move this Thread to the Java Forum, where it should belong ?
Copy linkTweet thisAlerts:
@HawksleyauthorFeb 18.2011 — Sorry about that Kor.
×

Success!

Help @Hawksley spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...