Source Code Cross Referenced for LengthSubstring.java in  » GIS » openjump » com » vividsolutions » jump » algorithm » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » GIS » openjump » com.vividsolutions.jump.algorithm 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.vividsolutions.jump.algorithm;
002:
003:        import com.vividsolutions.jts.geom.*;
004:        import com.vividsolutions.jts.util.Assert;
005:
006:        /**
007:         * Computes a substring of a {@link LineString}
008:         * between given distances along the line.
009:         * <ul>
010:         * <li>The distances are clipped to the actual line length
011:         * <li>If the start distance is equal to the end distance,
012:         * a zero-length line with two identical points is returned
013:         * <li>FUTURE: If the start distance is greater than the end distance,
014:         * an inverted section of the line is returned
015:         * </ul>
016:         * <p>
017:         * FUTURE: should handle startLength > endLength, and flip the returned
018:         * linestring. Also should handle negative lengths (they are measured from end
019:         * of line backwards).
020:         */
021:        // Martin made a decision to create this duplicate of a class from JCS. 
022:        // [Jon Aquino 2004-10-25]
023:        public class LengthSubstring {
024:            public static LineString getSubstring(LineString line,
025:                    double startLength, double endLength) {
026:                LengthSubstring ls = new LengthSubstring(line);
027:                return ls.getSubstring(startLength, endLength);
028:            }
029:
030:            private LineString line;
031:
032:            public LengthSubstring(LineString line) {
033:                this .line = line;
034:            }
035:
036:            public LineString getSubstring(double startDistance,
037:                    double endDistance) {
038:                // future: if start > end, flip values and return an inverted line
039:                Assert.isTrue(startDistance <= endDistance,
040:                        "inverted distances not currently supported");
041:
042:                Coordinate[] coordinates = line.getCoordinates();
043:                // check for a zero-length segment and handle appropriately
044:                if (endDistance <= 0.0) {
045:                    return line.getFactory()
046:                            .createLineString(
047:                                    new Coordinate[] { coordinates[0],
048:                                            coordinates[0] });
049:                }
050:                if (startDistance >= line.getLength()) {
051:                    return line.getFactory().createLineString(
052:                            new Coordinate[] {
053:                                    coordinates[coordinates.length - 1],
054:                                    coordinates[coordinates.length - 1] });
055:                }
056:                if (startDistance < 0.0) {
057:                    startDistance = 0.0;
058:                }
059:                return computeSubstring(startDistance, endDistance);
060:            }
061:
062:            /**
063:             * Assumes input is strictly valid (e.g. startDist < endDistance)
064:             *
065:             * @param startDistance
066:             * @param endDistance
067:             * @return
068:             */
069:            private LineString computeSubstring(double startDistance,
070:                    double endDistance) {
071:                Coordinate[] coordinates = line.getCoordinates();
072:                CoordinateList newCoordinates = new CoordinateList();
073:                double segmentStartDistance = 0.0;
074:                double segmentEndDistance = 0.0;
075:                boolean started = false;
076:                int i = 0;
077:                LineSegment segment = new LineSegment();
078:                while (i < coordinates.length - 1
079:                        && endDistance > segmentEndDistance) {
080:                    segment.p0 = coordinates[i];
081:                    segment.p1 = coordinates[i + 1];
082:                    i++;
083:                    segmentStartDistance = segmentEndDistance;
084:                    segmentEndDistance = segmentStartDistance
085:                            + segment.getLength();
086:
087:                    if (startDistance > segmentEndDistance)
088:                        continue;
089:                    if (startDistance >= segmentStartDistance
090:                            && startDistance < segmentEndDistance) {
091:                        newCoordinates.add(LocatePoint.pointAlongSegment(
092:                                segment.p0, segment.p1, startDistance
093:                                        - segmentStartDistance), false);
094:                    }
095:                    /*
096:                    if (startDistance >= segmentStartDistance
097:                        && startDistance == segmentEndDistance) {
098:                      newCoordinates.add(new Coordinate(segment.p1), false);
099:                    }
100:                     */
101:                    if (endDistance >= segmentEndDistance) {
102:                        newCoordinates.add(new Coordinate(segment.p1), false);
103:                    }
104:                    if (endDistance >= segmentStartDistance
105:                            && endDistance < segmentEndDistance) {
106:                        newCoordinates.add(LocatePoint.pointAlongSegment(
107:                                segment.p0, segment.p1, endDistance
108:                                        - segmentStartDistance), false);
109:                    }
110:                }
111:                Coordinate[] newCoordinateArray = newCoordinates
112:                        .toCoordinateArray();
113:                /**
114:                 * Ensure there is enough coordinates to build a valid line. Make a
115:                 * 2-point line with duplicate coordinates, if necessary There will
116:                 * always be at least one coordinate in the coordList.
117:                 */
118:                if (newCoordinateArray.length <= 1) {
119:                    newCoordinateArray = new Coordinate[] {
120:                            newCoordinateArray[0], newCoordinateArray[0] };
121:                }
122:                return line.getFactory().createLineString(newCoordinateArray);
123:            }
124:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.