Source Code Cross Referenced for TransformData.java in  » GIS » GeoTools-2.4.1 » org » geotools » demo » referencing » 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 » GeoTools 2.4.1 » org.geotools.demo.referencing 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *    Geotools2 - OpenSource mapping toolkit
003:         *    http://www.geotools.org
004:         *    (C) 2004,2005, Geotools Project Managment Committee (PMC)
005:         *
006:         *    This library is free software; you can redistribute it and/or
007:         *    modify it under the terms of the GNU Lesser General Public
008:         *    License as published by the Free Software Foundation;
009:         *    version 2.1 of the License.
010:         *
011:         *    This library is distributed in the hope that it will be useful,
012:         *    but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014:         *    Lesser General Public License for more details.
015:         *
016:         */
017:
018:        package org.geotools.demo.referencing;
019:
020:        //java dependancies
021:        import java.io.File;
022:        import java.net.URL;
023:        import java.util.Iterator;
024:
025:        //geotools dedendancies
026:        import org.geotools.data.DataStore;
027:        import org.geotools.data.FeatureSource;
028:        import org.geotools.feature.FeatureCollection;
029:        import org.geotools.data.FeatureWriter;
030:        import org.geotools.data.Transaction;
031:        import org.geotools.data.shapefile.ShapefileDataStore;
032:        import org.geotools.feature.Feature;
033:        import org.geotools.feature.FeatureType;
034:        import org.geotools.referencing.FactoryFinder;
035:
036:        //geoapi dependancies
037:        import org.opengis.referencing.crs.CRSFactory;
038:        import org.opengis.referencing.crs.CoordinateReferenceSystem;
039:        import org.opengis.referencing.operation.CoordinateOperationFactory;
040:        import org.opengis.referencing.operation.CoordinateOperation;
041:
042:        // JTS dependanceis
043:        import com.vividsolutions.jts.geom.CoordinateFilter;
044:        import com.vividsolutions.jts.geom.Geometry;
045:
046:        /**
047:         * A simple demo to transform the geometries in a shapefile from the source
048:         * coordinate reference system (CRS) to a target CRS. Source and target
049:         * CRS's are given here as well know text (WKT) strings. The default output path
050:         * is the users home directory.
051:         *
052:         * The following method is rather low-level. Typically the higher-level
053:         * DefaultQuery method demoed in org.geotools.demo.data.ShapeReprojector would
054:         * be used instead.
055:         *
056:         * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/demo/referencing/src/main/java/org/geotools/demo/referencing/TransformData.java $
057:         * @version $Id: TransformData.java 17699 2006-01-22 22:42:41Z desruisseaux $
058:         * @author rschulz
059:         */
060:        public class TransformData {
061:
062:            //hardcoded WKT strings
063:            private static String SOURCE_WKT = "GEOGCS[\"WGS84\", DATUM[\"WGS84\", SPHEROID[\"WGS84\", 6378137.0, 298.257223563]], PRIMEM[\"Greenwich\", 0.0], UNIT[\"degree\",0.017453292519943295], AXIS[\"Longitude\",EAST], AXIS[\"Latitude\",NORTH]]";
064:            private static String TARGET_WKT = "PROJCS[\"UTM Zone 14N\", GEOGCS[\"WGS84\", DATUM[\"WGS84\", SPHEROID[\"WGS84\", 6378137.0, 298.257223563]], PRIMEM[\"Greenwich\", 0.0], UNIT[\"degree\",0.017453292519943295], AXIS[\"Longitude\",EAST], AXIS[\"Latitude\",NORTH]], PROJECTION[\"Transverse_Mercator\"], PARAMETER[\"central_meridian\", -99.0], PARAMETER[\"latitude_of_origin\", 0.0], PARAMETER[\"scale_factor\", 0.9996], PARAMETER[\"false_easting\", 500000.0], PARAMETER[\"false_northing\", 0.0], UNIT[\"metre\",1.0], AXIS[\"x\",EAST], AXIS[\"y\",NORTH]]";
065:            //private static String TARGET_WKT = "PROJCS[\"Lambert\", GEOGCS[\"WGS84\", DATUM[\"WGS84\", SPHEROID[\"WGS84\", 6378137.0, 298.257223563]], PRIMEM[\"Greenwich\", 0.0], UNIT[\"degree\",0.017453292519943295], AXIS[\"Longitude\",EAST], AXIS[\"Latitude\",NORTH]], PROJECTION[\"Lambert_Conformal_Conic_2SP\"], PARAMETER[\"semi_major\", 6378137.0], PARAMETER[\"semi_minor\", 6356752.314245179], PARAMETER[\"central_meridian\", 0.0], PARAMETER[\"standard_parallel_1\", 20.0], PARAMETER[\"standard_parallel_2\", 20.0], PARAMETER[\"latitude_of_origin\", 0.0], PARAMETER[\"false_easting\", 100000.0], PARAMETER[\"false_northing\", 0.0], UNIT[\"metre\",1.0], AXIS[\"x\",EAST], AXIS[\"y\",NORTH]]";
066:
067:            //private static String WKT_2 = "PROJCS[\"Mercator\", GEOGCS[\"WGS84\", DATUM[\"WGS84\", SPHEROID[\"WGS84\", 6378137.0, 298.257223563]], PRIMEM[\"Greenwich\", 0.0], UNIT[\"degree\",0.017453292519943295], AXIS[\"Longitude\",EAST], AXIS[\"Latitude\",NORTH]], PROJECTION[\"Mercator_2SP\"], PARAMETER[\"semi_major\", 6378137.0], PARAMETER[\"semi_minor\", 6356752.314245179], PARAMETER[\"central_meridian\", 0.0], PARAMETER[\"standard_parallel_1\", 20.0], PARAMETER[\"false_easting\", 0.0], PARAMETER[\"false_northing\", 1000000.0], UNIT[\"metre\",1.0], AXIS[\"x\",EAST], AXIS[\"y\",NORTH]]";
068:            //private static String WKT_3 = "GEOGCS[\"Sphere\", DATUM[\"WGS84\", SPHEROID[\"WGS84\", 6370997.0, 0],TOWGS84[0,0,0,0,0,0,0]], PRIMEM[\"Greenwich\", 0.0], UNIT[\"degree\",0.017453292519943295], AXIS[\"Longitude\",EAST], AXIS[\"Latitude\",NORTH]]";
069:            //private static String WKT_4 = "PROJCS[\"TransverseMercator\", GEOGCS[\"Sphere\", DATUM[\"Sphere\", SPHEROID[\"Sphere\", 6370997.0, 0],TOWGS84[0,0,0,0,0,0,0]],PRIMEM[\"Greenwich\", 0.0], UNIT[\"degree\",0.017453292519943295], AXIS[\"Longitude\",EAST], AXIS[\"Latitude\",NORTH]], PROJECTION[\"Transverse_Mercator\"], PARAMETER[\"semi_major\", 6370997], PARAMETER[\"semi_minor\", 6370997], PARAMETER[\"central_meridian\", 0.0], PARAMETER[\"latitude_of_origin\", 0.0], PARAMETER[\"scale_factor\", 1.0], PARAMETER[\"false_easting\", 0.0], PARAMETER[\"false_northing\", 0.0], UNIT[\"metre\",1.0], AXIS[\"x\",EAST], AXIS[\"y\",NORTH]]";
070:
071:            /** Factory to create coordinate reference systems from WKT strings*/
072:            private CRSFactory crsFactory = FactoryFinder.getCRSFactory(null);
073:
074:            /** Factory to create transformations from a source and target CS */
075:            private CoordinateOperationFactory coFactory = FactoryFinder
076:                    .getCoordinateOperationFactory(null);
077:
078:            /** Creates a new instance of TransformData */
079:            public TransformData(URL inURL, URL outURL, String inWKT,
080:                    String outWKT) {
081:                FeatureCollection fc = null;
082:                Iterator i = null;
083:                FeatureWriter outFeatureWriter = null;
084:                try {
085:                    //create the CS's and transformation
086:                    CoordinateReferenceSystem inCRS = crsFactory
087:                            .createFromWKT(inWKT);
088:                    CoordinateReferenceSystem outCRS = crsFactory
089:                            .createFromWKT(outWKT);
090:                    System.out.println("source CRS: "
091:                            + inCRS.getName().getCode());
092:                    System.out.println("target CRS: "
093:                            + outCRS.getName().getCode());
094:                    CoordinateOperation co = coFactory.createOperation(inCRS,
095:                            outCRS);
096:                    System.out.println("transform: "
097:                            + co.getMathTransform().toWKT());
098:                    CoordinateFilter transFilter = new TransformationCoordinateFilter(
099:                            co.getMathTransform());
100:
101:                    //get the input shapefile
102:                    DataStore inStore = new ShapefileDataStore(inURL);
103:                    String name = inStore.getTypeNames()[0];
104:                    FeatureSource inSource = inStore.getFeatureSource(name);
105:                    fc = inSource.getFeatures();
106:                    FeatureType inSchema = inSource.getSchema();
107:
108:                    //create the output shapefile
109:                    DataStore outStore = new ShapefileDataStore(outURL);
110:                    Object[] outAttributes = new Object[inSchema
111:                            .getAttributeCount()];
112:                    outStore.createSchema(inSchema);
113:                    outFeatureWriter = outStore.getFeatureWriter(outStore
114:                            .getTypeNames()[0], Transaction.AUTO_COMMIT);
115:
116:                    for (i = fc.iterator(); i.hasNext();) {
117:                        Feature inFeature = (Feature) i.next();
118:                        // create a new feature
119:                        Feature outFeature = outFeatureWriter.next();
120:                        for (int j = 0; j < inFeature.getNumberOfAttributes(); j++) {
121:                            Object inAttribute = inFeature.getAttribute(j);
122:                            if (inAttribute instanceof  Geometry) {
123:                                Geometry geom = (Geometry) inAttribute;
124:                                geom.apply(transFilter);
125:                                outFeature.setAttribute(j, geom);
126:                            } else {
127:                                outFeature.setAttribute(j, inAttribute);
128:                            }
129:                        }
130:                        // write the new feature
131:                        outFeatureWriter.write();
132:                    }
133:
134:                    //close stuff
135:                    fc.close(i);
136:                    outFeatureWriter.close();
137:                    System.out.println("Done");
138:                } catch (Exception e) {
139:                    System.out.println("Exception: " + e);
140:                    e.printStackTrace();
141:                }
142:            }
143:
144:            /**
145:             * Command line arguments are:
146:             *   <ul>
147:             *     <li>in file - input shapefile
148:             *     <li>out file - output shapefile
149:             *     <li>in WKT - WKT string for the source coordinate system
150:             *     <li>out WKT - WKT string for the target coordinate system
151:             *   </ul>
152:             *
153:             * @param args the command line arguments
154:             * 
155:             */
156:            public static void main(String[] args) throws Exception {
157:                URL inURL, outURL;
158:
159:                if (args.length == 0) {
160:                    inURL = TransformData.class.getClassLoader().getResource(
161:                            "org/geotools/sampleData/statepop.shp");
162:                    outURL = new File(System.getProperty("user.home")
163:                            + "/statepopTransform.shp").toURL();
164:                    new TransformData(inURL, outURL, SOURCE_WKT, TARGET_WKT);
165:                } else if (args.length == 2) {
166:                    inURL = new File(args[0]).toURL();
167:                    outURL = new File(args[1]).toURL();
168:                    new TransformData(inURL, outURL, SOURCE_WKT, TARGET_WKT);
169:                } else if (args.length == 4) {
170:                    inURL = new File(args[0]).toURL();
171:                    outURL = new File(args[1]).toURL();
172:                    new TransformData(inURL, outURL, args[2], args[4]);
173:                } else {
174:                    System.out
175:                            .println("Usage: java TransformData <in file> <out file> <in WKT> <out WKT>");
176:                }
177:            }
178:
179:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.