Strabon
changeset 1510:4b2c4f8cb7fa
Removed deprecated file.
author | Panayiotis Smeros <psmeros@di.uoa.gr> |
---|---|
date | Wed Apr 29 12:58:44 2015 +0300 (2015-04-29) |
parents | c47780f6d3a1 |
children | f5e6db1a2ca6 |
files | runtime/src/main/java/eu/earthobservatory/utils/GeonamesParser.java |
line diff
1.1 --- a/runtime/src/main/java/eu/earthobservatory/utils/GeonamesParser.java Tue Apr 28 22:05:36 2015 +0300 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,392 +0,0 @@ 1.4 -/** 1.5 - * This Source Code Form is subject to the terms of the Mozilla Public 1.6 - * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 - * file, You can obtain one at http://mozilla.org/MPL/2.0/. 1.8 - * 1.9 - * Copyright (C) 2010, 2011, 2012, Pyravlos Team 1.10 - * 1.11 - * http://www.strabon.di.uoa.gr/ 1.12 - */ 1.13 -package eu.earthobservatory.utils; 1.14 - 1.15 -import info.aduna.iteration.CloseableIteration; 1.16 - 1.17 -import java.io.BufferedReader; 1.18 -import java.io.BufferedWriter; 1.19 -import java.io.ByteArrayInputStream; 1.20 -import java.io.File; 1.21 -import java.io.FileInputStream; 1.22 -import java.io.FileNotFoundException; 1.23 -import java.io.FileOutputStream; 1.24 -import java.io.IOException; 1.25 -import java.io.InputStreamReader; 1.26 -import java.io.OutputStreamWriter; 1.27 -import java.io.Serializable; 1.28 -import java.util.ArrayList; 1.29 -import java.util.HashMap; 1.30 -import java.util.List; 1.31 -import java.util.ListIterator; 1.32 -import java.util.Map; 1.33 - 1.34 -import org.geotools.data.DataUtilities; 1.35 -import org.geotools.data.shapefile.ShapefileDataStore; 1.36 -import org.geotools.data.shapefile.ShapefileDataStoreFactory; 1.37 -import org.geotools.data.simple.SimpleFeatureCollection; 1.38 -import org.geotools.data.simple.SimpleFeatureIterator; 1.39 -import org.geotools.data.simple.SimpleFeatureSource; 1.40 -import org.geotools.feature.FeatureCollections; 1.41 -import org.geotools.feature.SchemaException; 1.42 -import org.geotools.feature.simple.SimpleFeatureBuilder; 1.43 -import org.geotools.geometry.jts.JTSFactoryFinder; 1.44 -import org.opengis.feature.simple.SimpleFeature; 1.45 -import org.opengis.feature.simple.SimpleFeatureType; 1.46 -import org.opengis.geometry.MismatchedDimensionException; 1.47 -import org.opengis.referencing.FactoryException; 1.48 -import org.opengis.referencing.NoSuchAuthorityCodeException; 1.49 -import org.opengis.referencing.operation.TransformException; 1.50 -import org.openrdf.model.Namespace; 1.51 -import org.openrdf.model.Resource; 1.52 -import org.openrdf.model.Statement; 1.53 -import org.openrdf.model.URI; 1.54 -import org.openrdf.model.Value; 1.55 -import org.openrdf.model.impl.LiteralImpl; 1.56 -import org.openrdf.model.impl.StatementImpl; 1.57 -import org.openrdf.model.impl.URIImpl; 1.58 -import org.openrdf.repository.Repository; 1.59 -import org.openrdf.repository.RepositoryConnection; 1.60 -import org.openrdf.repository.RepositoryException; 1.61 -import org.openrdf.repository.sail.SailRepository; 1.62 -import org.openrdf.rio.RDFHandler; 1.63 -import org.openrdf.rio.RDFHandlerException; 1.64 -import org.openrdf.rio.RDFParseException; 1.65 -import org.openrdf.rio.RDFWriter; 1.66 -import org.openrdf.rio.turtle.TurtleWriter; 1.67 - 1.68 -import org.openrdf.sail.memory.MemoryStore; 1.69 - 1.70 -import com.vividsolutions.jts.geom.Coordinate; 1.71 -import com.vividsolutions.jts.geom.Geometry; 1.72 -import com.vividsolutions.jts.geom.GeometryFactory; 1.73 -import com.vividsolutions.jts.geom.MultiLineString; 1.74 -import com.vividsolutions.jts.geom.Point; 1.75 -import com.vividsolutions.jts.io.ParseException; 1.76 -import com.vividsolutions.jts.io.WKTReader; 1.77 - 1.78 -public class GeonamesParser { 1.79 - 1.80 - static int pointsOut = 0; 1.81 - static int e = 0, a = 0; 1.82 - 1.83 - public static void main(String[] args) throws IOException, SchemaException, 1.84 - NoSuchAuthorityCodeException, FactoryException, 1.85 - MismatchedDimensionException, TransformException { 1.86 - 1.87 - if (args.length < 2) { 1.88 - System.err.println("Usage: eu.earthobservatory.utils.GeonamesParser <IN_FILE> <OUT_FILE>"); 1.89 - System.exit(0); 1.90 - } 1.91 - final String inFile = args[0]; 1.92 - final String outFile = args[1]; 1.93 - final String baseURI = "http://www.geonames.org"; 1.94 - 1.95 - String greece = "POLYGON((" + 1.96 - "19.2214041948412 39.915644583545,19.2214041948412 39.915644583545,19.181097387078 39.9237059450676," + 1.97 - "19.5519200182563 40.0285036451009,19.6567177185669 39.9156445835922,20.1968289424263 39.9075832219288," + 1.98 - "20.3741788961446 40.1655467910226,20.6805106346546 40.4073876369722,20.8497992263774 40.94749885984," + 1.99 - "21.6962421887069 41.0845420054562,21.8897148655894 41.2538305976083,22.5910533201724 41.2780146817553," + 1.100 - "22.5829919583984 41.4231191894311,23.1150418204718 41.4473032736882,23.5987235131646 41.5279168887397," + 1.101 - "23.896993890336 41.5682236962285,24.3967983060956 41.6568986728098,24.7434368528031 41.5359782495121," + 1.102 - "25.331916245936 41.3989351029729,25.9687638080256 41.4553646333056,26.0574387848902 41.5440396101636," + 1.103 - "25.9284569997944 41.7536350102425,26.1944819306803 41.8423099869814,26.6297954543722 41.7375122866789," + 1.104 - "26.7426545167742 41.2941374019925,26.4685682243736 41.1812783406592,26.5169363940019 40.9716829406332," + 1.105 - "26.0896842326308 40.6008603101938,25.5415116482987 40.0688104490916,26.4927523118004 39.4642083330579," + 1.106 - "26.7265317973552 39.0288948098343,26.4040773361785 38.5693972024431,27.1537839611879 37.8599973865071," + 1.107 - "28.451663172698 36.4734432006192,28.0082882886197 35.6834297705082,29.5560697054562 36.3605841370316," + 1.108 - "29.9671991450442 36.0864978436166,27.0570476273072 34.6596368562464,24.0904665776989 34.5870846050009," + 1.109 - "21.9219603198949 35.538325268111,19.2214041948412 39.915644583545" + 1.110 - "))"; 1.111 - 1.112 - Repository myRepository = new SailRepository(new MemoryStore()); 1.113 - BufferedReader dis = null; 1.114 - RepositoryConnection conn = null; 1.115 - boolean isEven = false; 1.116 - 1.117 - Geometry greeceGeo = null; 1.118 - try { 1.119 - //mbbInWGS84 = new WKTReader().read("POLYGON((-10.6700 34.5000, 31.5500 34.5000, 31.5500 71.0500, -10.6700 71.0500, -10.6700 34.5000))"); 1.120 - greeceGeo = new WKTReader().read(greece); 1.121 - greeceGeo.setSRID(4326); 1.122 - } catch (ParseException e1) { 1.123 - e1.printStackTrace(); 1.124 - } 1.125 - 1.126 - // System.out.println("|--MBB Created--|"); 1.127 - 1.128 - // // -- Display map of Greece and its mbb -- // 1.129 - // SimpleFeatureCollection mbbCollection = createCollection(mbbInWGS84); 1.130 - // MapContext map = new DefaultMapContext(); 1.131 - // map.setTitle("Map"); 1.132 - // map.addLayer(clcFeatureCollection, null); 1.133 - // map.addLayer(mbbCollection, null); 1.134 - // JMapFrame.showMap(map); 1.135 - 1.136 - try { 1.137 - // Initialization of classes used for RDF handling 1.138 - myRepository.initialize(); 1.139 - conn = myRepository.getConnection(); 1.140 - conn.setAutoCommit(true); 1.141 - 1.142 - dis = new BufferedReader(new InputStreamReader( 1.143 - new FileInputStream(new File(inFile)))); 1.144 - BufferedWriter osw = new BufferedWriter(new OutputStreamWriter( 1.145 - new FileOutputStream(outFile), "UTF-8")); 1.146 - // BufferedWriter osw = new BufferedWriter(new 1.147 - // OutputStreamWriter(new FileOutputStream(outFile), "UTF-8")); 1.148 - RDFWriter wr = new TurtleWriter(osw); 1.149 - String line = null; 1.150 - 1.151 - // Adding data to repository 1.152 - wr.startRDF(); 1.153 - while (dis.ready()) { 1.154 - line = dis.readLine(); 1.155 - if (isEven) { 1.156 - try { 1.157 - conn.clear(); 1.158 - //ByteArrayInputStream bis = new ByteArrayInputStream(line.getBytes()); 1.159 - ByteArrayInputStream bis = new ByteArrayInputStream(line.getBytes("UTF-8")); 1.160 - conn.add(bis, baseURI, org.openrdf.rio.RDFFormat.RDFXML); 1.161 - exportGeoname(wr, conn, greeceGeo); 1.162 - } catch (RDFParseException ex) { 1.163 - System.err.println(line); 1.164 - e++; 1.165 - } 1.166 - } else { 1.167 - a++; 1.168 - } 1.169 - isEven = !isEven; 1.170 - } 1.171 - wr.endRDF(); 1.172 - dis.close(); 1.173 - 1.174 - System.out.println("Rejected features: " + e); 1.175 - System.out.println("Parsed features: " + a); 1.176 - System.out.println("Points out of MBB of Greece: " + pointsOut); 1.177 - } catch (RepositoryException e) { 1.178 - e.printStackTrace(); 1.179 - } catch (FileNotFoundException e) { 1.180 - e.printStackTrace(); 1.181 - } catch (IOException e) { 1.182 - e.printStackTrace(); 1.183 - } catch (RDFHandlerException e) { 1.184 - e.printStackTrace(); 1.185 - } 1.186 - 1.187 - } 1.188 - 1.189 - /** 1.190 - * Create a SimpleFeatureCollection with a Geometry 1.191 - * 1.192 - * @param all 1.193 - * @return 1.194 - * @throws SchemaException 1.195 - */ 1.196 - public static SimpleFeatureCollection createCollection(Geometry g) 1.197 - throws SchemaException { 1.198 - 1.199 - SimpleFeatureCollection collection = FeatureCollections.newCollection(); 1.200 - SimpleFeatureType TYPE = DataUtilities.createType("MBB", 1.201 - "location:Polygon:srid=4326"); // 4326 = srid of wgs84 1.202 - SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE); 1.203 - 1.204 - featureBuilder.add(g); 1.205 - SimpleFeature feature = featureBuilder.buildFeature(null); 1.206 - collection.add(feature); 1.207 - 1.208 - return collection; 1.209 - } 1.210 - 1.211 -// public static Geometry getMBB(String filename) throws IOException, 1.212 -// SchemaException, NoSuchAuthorityCodeException, FactoryException { 1.213 -// SimpleFeatureCollection featureCollection = getFeatureCollection( 1.214 -// filename, "ISO8859-7"); 1.215 -// 1.216 -// SimpleFeatureIterator featureIterator = featureCollection.features(); 1.217 -// 1.218 -// MultiPolygon all = null; 1.219 -// // Iterate features of shp file 1.220 -// while (featureIterator.hasNext()) { 1.221 -// SimpleFeature f = featureIterator.next(); 1.222 -// List<Object> attributes = f.getAttributes(); 1.223 -// 1.224 -// MultiPolygon landItem = (MultiPolygon) attributes.get(0); 1.225 -// 1.226 -// if (all == null) 1.227 -// all = landItem; 1.228 -// else 1.229 -// all = (MultiPolygon) all.union(landItem); 1.230 -// } 1.231 -// 1.232 -// return all.getEnvelope(); 1.233 -// } 1.234 - 1.235 - /** 1.236 - * @param filename 1.237 - * shp file to be opened 1.238 - * @param encoding 1.239 - * encoding of opened shp file 1.240 - * @return a feature collection of opened shp file 1.241 - * @throws IOException 1.242 - * @throws FactoryException 1.243 - * @throws NoSuchAuthorityCodeException 1.244 - */ 1.245 - public static SimpleFeatureCollection getFeatureCollection(String filename, 1.246 - String encoding) throws IOException, NoSuchAuthorityCodeException, 1.247 - FactoryException { 1.248 - // Open file 1.249 - File infile = new File(filename); 1.250 - 1.251 - // Parameters of ShapefileDatastore 1.252 - Map<String, Serializable> params = new HashMap<String, Serializable>(); 1.253 - params.put(ShapefileDataStoreFactory.URLP.key, infile.toURI().toURL()); 1.254 - // params.put("create spatial index", Boolean.TRUE); 1.255 - params.put(ShapefileDataStoreFactory.DBFCHARSET.key, encoding); 1.256 - 1.257 - // -- Create ShapefileDatastore -- // 1.258 - ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory(); 1.259 - ShapefileDataStore shapefile = (ShapefileDataStore) dataStoreFactory 1.260 - .createNewDataStore(params); 1.261 - // shapefile.setStringCharset(Charset.forName("ISO8859-7")); 1.262 - // DataStore shapefile = new ShapefileDataStore( infile.toURI().toURL(), false); 1.263 - 1.264 - // Get feature collection 1.265 - String[] typeName = shapefile.getTypeNames(); 1.266 - SimpleFeatureSource featureSource = shapefile 1.267 - .getFeatureSource(typeName[0]); 1.268 - //SimpleFeatureCollection collection; 1.269 - SimpleFeatureCollection featureCollection = featureSource.getFeatures(); 1.270 - 1.271 - // SimpleFeatureIterator clcFeatureIterator = 1.272 - // clcFeatureCollection.features(); 1.273 - return featureCollection; 1.274 - } 1.275 - 1.276 - // public static void showMap(String filename) throws IOException { 1.277 - // 1.278 - // File file = new File(filename); 1.279 - // 1.280 - // FileDataStore store = FileDataStoreFinder.getDataStore(file); 1.281 - // SimpleFeatureSource featureSource = store.getFeatureSource(); 1.282 - // 1.283 - // // Create a map context and add our shapefile to it 1.284 - // MapContext map = new DefaultMapContext(); 1.285 - // map.setTitle("Quickstart"); 1.286 - // map.addLayer(featureSource, null); 1.287 - // 1.288 - // // Now display the map 1.289 - // JMapFrame.showMap(map); 1.290 - // } 1.291 - 1.292 - /** 1.293 - * @param featureIterator 1.294 - * iterator of features to print 1.295 - * @throws IOException 1.296 - */ 1.297 - public static void printFeatures(SimpleFeatureIterator featureIterator) 1.298 - throws IOException { 1.299 - 1.300 - // Iterate features of shp file 1.301 - while (featureIterator.hasNext()) { 1.302 - SimpleFeature f = featureIterator.next(); 1.303 - List<Object> attributes = f.getAttributes(); 1.304 - 1.305 - for (int i = 0; i < attributes.size(); i++) { 1.306 - // MultiLineString geometry = 1.307 - // (MultiLineString)attributes.get(0); 1.308 - Object attribute = attributes.get(i); 1.309 - if (!(attribute instanceof MultiLineString)) 1.310 - System.out.println(attributes.get(i).toString()); 1.311 - } 1.312 - System.out.println("---------"); 1.313 - } 1.314 - } 1.315 - 1.316 - public static void exportGeoname(RDFHandler handler, 1.317 - RepositoryConnection conn, Geometry greeceGeo) 1.318 - throws RepositoryException, RDFHandlerException { 1.319 - 1.320 - final URI hasGeography = new URIImpl( 1.321 - "http://teleios.di.uoa.gr/ontologies/noaOntology.owl#hasGeography"); 1.322 - final URI geometry = new URIImpl("http://strdf.di.uoa.gr/ontology#WKT"); 1.323 - URI latPredicate = new URIImpl( 1.324 - "http://www.w3.org/2003/01/geo/wgs84_pos#lat"); 1.325 - URI longPredicate = new URIImpl( 1.326 - "http://www.w3.org/2003/01/geo/wgs84_pos#long"); 1.327 - 1.328 - handler.handleNamespace("geo", "http://www.example.org/geo#"); 1.329 - handler.handleNamespace("strdf", "http://strdf.di.uoa.gr/ontology#"); 1.330 - handler.handleNamespace("noa", 1.331 - "http://teleios.di.uoa.gr/ontologies/noaOntology.owl#"); 1.332 - 1.333 - // Export namespace information 1.334 - CloseableIteration<? extends Namespace, RepositoryException> nsIter = conn 1.335 - .getNamespaces(); 1.336 - try { 1.337 - while (nsIter.hasNext()) { 1.338 - Namespace ns = nsIter.next(); 1.339 - handler.handleNamespace(ns.getPrefix(), ns.getName()); 1.340 - } 1.341 - } finally { 1.342 - nsIter.close(); 1.343 - } 1.344 - 1.345 - // Export statements 1.346 - CloseableIteration<? extends Statement, RepositoryException> stIter = conn 1.347 - .getStatements(null, null, null, false); 1.348 - 1.349 - try { 1.350 - Value latV = null, longV = null; 1.351 - Statement st = null; 1.352 - ArrayList<Statement> stL = new ArrayList<Statement>(); 1.353 - Resource sub = null; 1.354 - while (stIter.hasNext()) { 1.355 - st = stIter.next(); 1.356 - URI p = st.getPredicate(); 1.357 - 1.358 - if (p.equals(latPredicate)) { 1.359 - sub = st.getSubject(); 1.360 - latV = st.getObject(); 1.361 - } else if (p.equals(longPredicate)) { 1.362 - longV = st.getObject(); 1.363 - } else { 1.364 - stL.add(st); 1.365 - } 1.366 - } 1.367 - st = new StatementImpl(sub, hasGeography, new LiteralImpl("POINT(" 1.368 - + longV.stringValue() + " " + latV.stringValue() + ")", 1.369 - geometry) // TODO einai swsth h seira, nomizw nai ?? 1.370 - ); 1.371 - stL.add(st); 1.372 - 1.373 - GeometryFactory geometryFactory = JTSFactoryFinder 1.374 - .getGeometryFactory(null); 1.375 - Point point = geometryFactory.createPoint(new Coordinate(Double.parseDouble( 1.376 - //latV.stringValue()), Double.parseDouble(longV.stringValue())) 1.377 - longV.stringValue()), Double.parseDouble(latV.stringValue())) 1.378 - ); 1.379 - // TODO To parapanw nomizw einai swsto edw giati ta pairnei 1.380 - // anapoda?? 1.381 - 1.382 - if (greeceGeo.contains(point)) { 1.383 - ListIterator<Statement> stLI = stL.listIterator(); 1.384 - while (stLI.hasNext()) { 1.385 - handler.handleStatement(stLI.next()); 1.386 - } 1.387 - } else 1.388 - pointsOut++; 1.389 - 1.390 - System.out.println(a + ": " + e + " - " + pointsOut); 1.391 - } finally { 1.392 - stIter.close(); 1.393 - } 1.394 - } 1.395 -} 1.396 \ No newline at end of file