Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java @ 1106:e9f66d63bdd0
Write user defined barriers into a shapefile that is placed in the Artifact's directory.
flys-artifacts/trunk@2609 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Tue, 30 Aug 2011 11:09:54 +0000 |
parents | eeebf8514c7f |
children | 005c2964f9af |
comparison
equal
deleted
inserted
replaced
1105:adb52a2005e7 | 1106:e9f66d63bdd0 |
---|---|
1 package de.intevation.flys.utils; | 1 package de.intevation.flys.utils; |
2 | |
3 import java.io.IOException; | |
4 import java.io.File; | |
5 import java.io.Serializable; | |
6 import java.net.MalformedURLException; | |
7 import java.util.ArrayList; | |
8 import java.util.HashMap; | |
9 import java.util.List; | |
10 import java.util.Map; | |
2 | 11 |
3 import com.vividsolutions.jts.geom.Coordinate; | 12 import com.vividsolutions.jts.geom.Coordinate; |
4 import com.vividsolutions.jts.geom.Geometry; | 13 import com.vividsolutions.jts.geom.Geometry; |
5 | 14 |
15 import org.opengis.feature.simple.SimpleFeature; | |
16 import org.opengis.feature.simple.SimpleFeatureType; | |
17 | |
18 import org.geotools.data.DataStoreFactorySpi; | |
19 import org.geotools.data.DataUtilities; | |
20 import org.geotools.data.FeatureStore; | |
21 import org.geotools.data.DefaultTransaction; | |
22 import org.geotools.data.Transaction; | |
23 import org.geotools.data.shapefile.ShapefileDataStore; | |
24 import org.geotools.data.shapefile.ShapefileDataStoreFactory; | |
25 import org.geotools.feature.FeatureIterator; | |
26 import org.geotools.feature.FeatureCollection; | |
27 import org.geotools.feature.SchemaException; | |
28 import org.geotools.feature.simple.SimpleFeatureTypeBuilder; | |
29 import org.geotools.geojson.feature.FeatureJSON; | |
30 import org.geotools.referencing.crs.DefaultGeographicCRS; | |
6 | 31 |
7 | 32 |
8 public class GeometryUtils { | 33 public class GeometryUtils { |
9 | 34 |
10 private GeometryUtils() { | 35 private GeometryUtils() { |
26 return null; | 51 return null; |
27 } | 52 } |
28 | 53 |
29 return "" + c[0].x + " " + c[1].y + " " + c[1].x + " " + c[0].y; | 54 return "" + c[0].x + " " + c[1].y + " " + c[1].x + " " + c[0].y; |
30 } | 55 } |
56 | |
57 | |
58 /** | |
59 * Returns a SimpleFeatureType used while parsing the GeoJSON string that | |
60 * represents the barriers entered by the user. | |
61 * | |
62 * @param srs The SRS that is used by the GeoJSON string. | |
63 * | |
64 * @return a SimpleFeatureType. | |
65 */ | |
66 public static SimpleFeatureType buildBarriersFeatureType(String srs) { | |
67 SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); | |
68 | |
69 builder.setName("flys"); | |
70 builder.setNamespaceURI("http://www.intevation.de/"); | |
71 builder.setSRS(srs); | |
72 builder.setDefaultGeometry("geometry"); | |
73 | |
74 builder.add("geometry", org.opengis.geometry.Geometry.class); | |
75 | |
76 return builder.buildFeatureType(); | |
77 } | |
78 | |
79 | |
80 public static SimpleFeatureType buildFeatureType( | |
81 String name, String srid, String geometryType) | |
82 throws SchemaException | |
83 { | |
84 String schema = "geom:" + geometryType + ":srid=" + srid; | |
85 | |
86 return DataUtilities.createType(name, schema); | |
87 } | |
88 | |
89 | |
90 public static List<SimpleFeature> parseGeoJSON( | |
91 String geojson, SimpleFeatureType ft | |
92 ) { | |
93 List<SimpleFeature> collection = new ArrayList<SimpleFeature>(); | |
94 | |
95 try { | |
96 FeatureJSON fjson = new FeatureJSON(); | |
97 fjson.setFeatureType(ft); | |
98 | |
99 FeatureIterator<SimpleFeature> iterator = | |
100 fjson.streamFeatureCollection(geojson); | |
101 | |
102 while (iterator.hasNext()) { | |
103 collection.add(iterator.next()); | |
104 } | |
105 } | |
106 catch (IOException ioe) { | |
107 // TODO handle exception | |
108 } | |
109 | |
110 return collection; | |
111 } | |
112 | |
113 | |
114 public static boolean writeShapefile( | |
115 File shape, | |
116 SimpleFeatureType featureType, | |
117 FeatureCollection collection) | |
118 throws MalformedURLException, IOException | |
119 { | |
120 Map<String, Serializable> params = new HashMap<String, Serializable>(); | |
121 params.put("url", shape.toURI().toURL()); | |
122 params.put("create spatial index", Boolean.TRUE); | |
123 | |
124 DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory(); | |
125 | |
126 ShapefileDataStore newDataStore = | |
127 (ShapefileDataStore)dataStoreFactory.createNewDataStore(params); | |
128 | |
129 newDataStore.createSchema(featureType); | |
130 newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84); | |
131 | |
132 Transaction transaction = new DefaultTransaction("create"); | |
133 | |
134 String typeName = newDataStore.getTypeNames()[0]; | |
135 | |
136 FeatureStore<SimpleFeatureType, SimpleFeature> featureStore = | |
137 (FeatureStore<SimpleFeatureType, SimpleFeature>) | |
138 newDataStore.getFeatureSource(typeName); | |
139 | |
140 featureStore.setTransaction(transaction); | |
141 | |
142 featureStore.addFeatures(collection); | |
143 transaction.commit(); | |
144 | |
145 return true; | |
146 } | |
31 } | 147 } |
32 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 148 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |