Changeset 33790 for other-projects
- Timestamp:
- 2019-12-10T20:43:53+13:00 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
other-projects/maori-lang-detection/src/org/greenstone/atea/CountryCodeCountsMapData.java
r33778 r33790 14 14 15 15 // For working with GeoJSON's Simple Features in Java 16 import mil.nga.sf.geojson.FeatureConverter; 16 17 import mil.nga.sf.geojson.Geometry; 17 18 import mil.nga.sf.geojson.MultiPoint; … … 19 20 20 21 21 // Simple Features GeoJSON Java 22 // https://ngageoint.github.io/simple-features-geojson-java/ 23 // https://mvnrepository.com/artifact/mil.nga.sf/sf-geojson 22 /** Simple Features GeoJSON Java 23 * https://ngageoint.github.io/simple-features-geojson-java/ - liks to API and more 24 * 25 * https://mvnrepository.com/artifact/mil.nga.sf/sf-geojson (https://github.com/ngageoint/simple-features-geojson-java/) 26 * 27 * Also need the basic data types used by the Geometry objects above: 28 * https://mvnrepository.com/artifact/mil.nga/sf (https://github.com/ngageoint/simple-features-java) 29 * 30 * Further helper jars needed (because of encountering the exception documented at 31 * stackoverflow.com/questions/36278293/java-lang-classnotfoundexception-com-fasterxml-jackson-core-jsonprocessingexcep/36279872) 32 * https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core/2.10.0 33 * https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind 34 * https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations/2.10.0 35 */ 24 36 25 37 /** 26 * This class needs the gson library. I copied the gson jar file from GS3. 38 * This class needs the gson library, and now the sf(-2.02).jar and sf-geojson(-2.02).jar files too 39 * to create and store Simple Features geo json objects with Java. 40 * I copied the gson jar file from GS3. 27 41 * 28 42 * TO COMPILE: … … 38 52 static Logger logger = Logger.getLogger(org.greenstone.atea.CountryCodeCountsMapData.class.getName()); 39 53 40 Map<String, JsonObject> countryToJsonMap;54 //Map<String, JsonObject> countryToJsonMap; 41 55 JsonArray countryCodesJsonArray; 42 56 JsonArray countryCountsJsonArray; 57 58 // North-central Antarctica coords 59 private final double ANTARCTICA_LNG = 57.0d; 60 private final double ANTARCTICA_LAT = -70.0d; 61 // For EU coords, spot in Atlantic Ocean close to western European coast. 62 private final double EU_LNG = -20.0d; 63 private final double EU_LAT = 50.0d; 43 64 44 65 public CountryCodeCountsMapData(String countryCoordsJSONFile, String countryCountsJSONFile) throws Exception { 45 66 46 67 // map of country codes to lat, lng json for that country code 47 countryToJsonMap = new HashMap<String, JsonObject>(); 48 68 Map<String, JsonObject> countryToJsonMap = new HashMap<String, JsonObject>(); 69 70 // Parse json file of country codes and put into a JsonArray. 71 // then put into map of each country code to its JsonObject. 49 72 countryCodesJsonArray = parseJSONFile(countryCoordsJSONFile); 50 73 for(JsonElement obj : countryCodesJsonArray) { … … 53 76 } 54 77 55 78 // Parse json file of country code counts 79 // Then for each JsonObject in this file, 80 // find a match on its country code in the map created above to get a country code JsonObject 81 // Get the longitude and latitude of the JsonObject that matched that country code. 82 // Add this lng,lat location information to the current JsonObject from the counts file. 56 83 countryCountsJsonArray = parseJSONFile(countryCountsJSONFile); 57 84 … … 61 88 int count = (int)json.get("count").getAsDouble(); 62 89 63 System.err.println("Got country code: " + countryCode);64 System.err.println(" count: " + count);90 //logger.info("Got country code: " + countryCode); 91 //logger.info(" count: " + count); 65 92 66 93 // locate in countryCode map 67 94 JsonObject countryCodeJson = countryToJsonMap.get(countryCode); 68 95 69 System.err.println("Found in map: " + countryCodeJson.toString()); 70 71 // for geojson, want longitude then latitude 72 Double lng = countryCodeJson.get("longitude").getAsDouble(); 73 Double lat = countryCodeJson.get("latitude").getAsDouble(); 74 System.err.println("long: " + Double.toString(lng) + ", lat: " + Double.toString(lat)); 75 76 // let's add lat and lng fields to countryCounts object 77 json.addProperty("lng", lng); // adds Number: https://javadoc.io/static/com.google.code.gson/gson/2.8.5/index.html?com/google/gson/Gson.html 78 json.addProperty("lat", lat); 79 } 80 81 } 82 83 96 if(countryCodeJson != null) { 97 //logger.info("Found in map: " + countryCodeJson.toString()); 98 99 // for geojson, want longitude then latitude 100 Double lng = countryCodeJson.get("longitude").getAsDouble(); 101 Double lat = countryCodeJson.get("latitude").getAsDouble(); 102 //logger.info("long: " + Double.toString(lng) + ", lat: " + Double.toString(lat)); 103 104 // let's add lat and lng fields to countryCounts object 105 json.addProperty("lng", lng); // adds Number: https://javadoc.io/static/com.google.code.gson/gson/2.8.5/index.html?com/google/gson/Gson.html 106 json.addProperty("lat", lat); 107 } else { 108 logger.info("No geolocation info found for country code " + countryCode); 109 if(countryCode.equals("EU")) { 110 //logger.info("Unlisted country code: EU"); 111 // add lat and lng for Europe 112 json.addProperty("lng", EU_LNG); 113 json.addProperty("lat", EU_LAT); 114 } 115 else if(countryCode.equals("UNKNOWN")) { 116 //logger.info("Unlisted country code: UNKNOWN"); 117 // add lat and lng for Antarctica 118 json.addProperty("lng", ANTARCTICA_LNG); 119 json.addProperty("lat", ANTARCTICA_LAT); 120 } else { 121 logger.error("ERROR: entirely unknown country code: " + countryCode); 122 } 123 } 124 } 125 126 } 127 128 /** Convert mongodb tabular output of json records stored in the given file 129 * into a JsonArray. 130 */ 84 131 public JsonArray parseJSONFile(String filename) throws Exception { 85 132 JsonArray jsonArray = null; … … 100 147 // replace last comma with closing bracket 101 148 String fileContents = str.substring(0, str.length()-2) + "]"; 102 //String fileContents = str.substring(0, str.length()-2); 103 System.err.println("Got file:\n" + fileContents); 149 150 //System.err.println("Got file:\n" + fileContents); 151 104 152 // https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java 105 153 jsonArray = new JsonParser().parse(fileContents).getAsJsonArray(); … … 131 179 * has it. So I've been trying to build that, but don't have the correct version of maven. 132 180 */ 133 public Geometry toMultiPointGeoJSON() { 134 System.err.println("toGeoJSON() is not yet implemented."); 135 136 //MultiPoint multiPoint = null; 137 181 public Geometry toMultiPointGeoJson() { 182 //System.err.println("toGeoJSON() is not yet implemented."); 183 138 184 List<Position> points = new LinkedList<Position>(); 139 185 140 //Geometry multipoint = new MultiPoint();141 186 for(JsonElement obj : countryCountsJsonArray) { 142 187 JsonObject json = obj.getAsJsonObject(); 143 Double lng = json.get("l ongitude").getAsDouble();144 Double lat = json.get("lat itude").getAsDouble();188 Double lng = json.get("lng").getAsDouble(); 189 Double lat = json.get("lat").getAsDouble(); 145 190 146 191 Position point = new Position(lng, lat); 147 192 points.add(point); 148 193 } 149 150 //Geometry multiPoint = new mil.nga.sf.geojson.MultiPoint(points); 194 151 195 Geometry multiPoint = new MultiPoint(points); 152 196 153 197 return multiPoint; 154 155 156 } 157 158 198 } 199 200 201 public String writeMultiPointGeoJsonToFile(File folder) { 202 final String filename = "multipoint.json"; 203 File outFile = new File(folder, filename); 204 205 Geometry geometry = this.toMultiPointGeoJson(); 206 String multiPointGeojsonString = FeatureConverter.toStringValue(geometry); 207 System.err.println("\nMap data as MultiPoint geometry:\n" + multiPointGeojsonString + "\n"); 208 try ( 209 Writer writer = new BufferedWriter(new FileWriter(outFile)); 210 ) { 211 212 // Some basic re-formatting for some immediate legibility 213 // But pasting the contents of the file (or the System.err output above) 214 // directly into http://geojson.tools/ will instantly reformat the json perfectly anyway. 215 multiPointGeojsonString = multiPointGeojsonString.replace("[[", "\n[\n\t["); 216 multiPointGeojsonString = multiPointGeojsonString.replace("],[", "],\n\t["); 217 multiPointGeojsonString = multiPointGeojsonString.replace("]]", "]\n]"); 218 219 writer.write(multiPointGeojsonString + "\n"); 220 } catch(Exception e) { 221 logger.error("Unable to write multipoint geojson:\n**********************"); 222 logger.error(multiPointGeojsonString); 223 logger.error("**********************\ninto file " + outFile.getAbsolutePath()); 224 logger.error(e.getMessage(), e); 225 } 226 227 return outFile.getAbsolutePath(); 228 229 } 230 231 232 // Unfinished and unused 159 233 public void parseCSVFile(String filename) throws Exception { 160 234 File csvData = new File(filename); 161 235 CSVParser parser = CSVParser.parse(csvData, java.nio.charset.Charset.forName("US-ASCII"), CSVFormat.RFC4180); 162 236 for (CSVRecord csvRecord : parser) { 163 System.err.println("Got record: " + csvRecord.toString());237 logger.info("Got record: " + csvRecord.toString()); 164 238 } 165 239 } … … 174 248 System.exit(-1); 175 249 } 176 250 177 251 try { 252 File countsFile = new File(args[1]); 253 File parentFolder = countsFile.getParentFile().getCanonicalFile(); // canonical resolves any .. and . in path 254 178 255 CountryCodeCountsMapData mapData = new CountryCodeCountsMapData(args[0], args[1]); 179 //mapData.toMultiPointGeoJSON(); 256 257 //Geometry geometry = mapData.toMultiPointGeoJSON(); 258 //String multiPointGeojsonString = FeatureConverter.toStringValue(geometry); 259 //System.err.println("geometry: " + multiPointGeojsonString); 260 261 String outFileName = mapData.writeMultiPointGeoJsonToFile(parentFolder); 262 System.err.println("***********\nWrote mapdata to file " + outFileName); 180 263 } catch(Exception e) { 181 e.printStackTrace();264 logger.error(e.getMessage(), e); 182 265 } 183 266 }
Note:
See TracChangeset
for help on using the changeset viewer.