root / trunk / libraries / libTopology / src / org / gvsig / jts / RobustLengthIndexedLine.java @ 13591
History | View | Annotate | Download (4.11 KB)
1 | 13591 | azabala | /*
|
---|---|---|---|
2 | * Created on 07-sep-2007
|
||
3 | *
|
||
4 | * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
||
5 | *
|
||
6 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
7 | *
|
||
8 | * This program is free software; you can redistribute it and/or
|
||
9 | * modify it under the terms of the GNU General Public License
|
||
10 | * as published by the Free Software Foundation; either version 2
|
||
11 | * of the License, or (at your option) any later version.
|
||
12 | *
|
||
13 | * This program is distributed in the hope that it will be useful,
|
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
16 | * GNU General Public License for more details.
|
||
17 | *
|
||
18 | * You should have received a copy of the GNU General Public License
|
||
19 | * along with this program; if not, write to the Free Software
|
||
20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
21 | *
|
||
22 | * For more information, contact:
|
||
23 | *
|
||
24 | * Generalitat Valenciana
|
||
25 | * Conselleria d'Infraestructures i Transport
|
||
26 | * Av. Blasco Ib??ez, 50
|
||
27 | * 46010 VALENCIA
|
||
28 | * SPAIN
|
||
29 | *
|
||
30 | * +34 963862235
|
||
31 | * gvsig@gva.es
|
||
32 | * www.gvsig.gva.es
|
||
33 | *
|
||
34 | * or
|
||
35 | *
|
||
36 | * IVER T.I. S.A
|
||
37 | * Salamanca 50
|
||
38 | * 46005 Valencia
|
||
39 | * Spain
|
||
40 | *
|
||
41 | * +34 963163400
|
||
42 | * dac@iver.es
|
||
43 | */
|
||
44 | /* CVS MESSAGES:
|
||
45 | *
|
||
46 | * $Id$
|
||
47 | * $Log$
|
||
48 | * Revision 1.1 2007-09-09 11:44:09 azabala
|
||
49 | * Improvements to JTS (robustness problems)
|
||
50 | *
|
||
51 | *
|
||
52 | */
|
||
53 | package org.gvsig.jts; |
||
54 | |||
55 | import com.vividsolutions.jts.geom.Coordinate; |
||
56 | import com.vividsolutions.jts.geom.Geometry; |
||
57 | import com.vividsolutions.jts.geom.LineSegment; |
||
58 | import com.vividsolutions.jts.geom.PrecisionModel; |
||
59 | import com.vividsolutions.jts.linearref.LengthIndexedLine; |
||
60 | import com.vividsolutions.jts.linearref.LinearIterator; |
||
61 | |||
62 | /**
|
||
63 | * Implementation of LengthIndexedLine to avoid robustness problems.
|
||
64 | *
|
||
65 | * TODO If JTS official implementation of LenghtIndexedLine solve the problem
|
||
66 | * that has been already reported about precission robustness, DELETE THIS CLASS
|
||
67 | *
|
||
68 | * @author azabala
|
||
69 | *
|
||
70 | */
|
||
71 | public class RobustLengthIndexedLine extends LengthIndexedLine { |
||
72 | |||
73 | /**
|
||
74 | * Superclass has no methods to expose linearGeom, and is private, so we
|
||
75 | * need to mantain a reference here
|
||
76 | */
|
||
77 | private Geometry linearGeom;
|
||
78 | |||
79 | public RobustLengthIndexedLine(Geometry arg0) {
|
||
80 | super(arg0);
|
||
81 | this.linearGeom = arg0;
|
||
82 | |||
83 | } |
||
84 | |||
85 | public double indexOf(Coordinate pt) { |
||
86 | return indexOfFromStart(pt, -1); |
||
87 | } |
||
88 | |||
89 | public double indexOfAfter(Coordinate pt, double minIndex) { |
||
90 | if (minIndex < 0.0) |
||
91 | return indexOf(pt);
|
||
92 | double endIndex = linearGeom.getLength();
|
||
93 | if (endIndex < minIndex)
|
||
94 | return endIndex;
|
||
95 | double closestAfter = indexOfFromStart(pt, minIndex);
|
||
96 | return closestAfter;
|
||
97 | } |
||
98 | |||
99 | /**
|
||
100 | * Return the first index ocurrence of the given coordinate grater than
|
||
101 | * minIndex.
|
||
102 | */
|
||
103 | |||
104 | private double indexOfFromStart(Coordinate inputPt, double minIndex) { |
||
105 | double minDistance = Double.MAX_VALUE; |
||
106 | double ptMeasure = minIndex;
|
||
107 | double segmentStartMeasure = 0.0; |
||
108 | LineSegment seg = new LineSegment();
|
||
109 | LinearIterator it = new LinearIterator(linearGeom);
|
||
110 | while (it.hasNext()) {
|
||
111 | if (!it.isEndOfLine()) {
|
||
112 | seg.p0 = it.getSegmentStart(); |
||
113 | seg.p1 = it.getSegmentEnd(); |
||
114 | double segDistance = seg.distance(inputPt);
|
||
115 | /*
|
||
116 | * these two lines of code are the only novelties respect to LenghtIndexedLine
|
||
117 | */
|
||
118 | PrecisionModel pm = linearGeom.getPrecisionModel(); |
||
119 | segDistance = pm.makePrecise(segDistance); |
||
120 | /*
|
||
121 | * FIXME Remove them if JTS solve the problem
|
||
122 | */
|
||
123 | double segMeasureToPt = segmentNearestMeasure(seg, inputPt,
|
||
124 | segmentStartMeasure); |
||
125 | |||
126 | if (segDistance < minDistance && segMeasureToPt > minIndex) {
|
||
127 | ptMeasure = segMeasureToPt; |
||
128 | minDistance = segDistance; |
||
129 | } |
||
130 | segmentStartMeasure += seg.getLength(); |
||
131 | } |
||
132 | it.next(); |
||
133 | } |
||
134 | return ptMeasure;
|
||
135 | } |
||
136 | |||
137 | private double segmentNearestMeasure(LineSegment seg, Coordinate inputPt, |
||
138 | double segmentStartMeasure) {
|
||
139 | // found new minimum, so compute location distance of point
|
||
140 | double projFactor = seg.projectionFactor(inputPt);
|
||
141 | if (projFactor <= 0.0) |
||
142 | return segmentStartMeasure;
|
||
143 | if (projFactor <= 1.0) |
||
144 | return segmentStartMeasure + projFactor * seg.getLength();
|
||
145 | // projFactor > 1.0
|
||
146 | return segmentStartMeasure + seg.getLength();
|
||
147 | } |
||
148 | |||
149 | } |