Changeset 33794 for other-projects
- Timestamp:
- 2019-12-11T21:57:02+13:00 (4 years ago)
- Location:
- other-projects/maori-lang-detection
- Files:
-
- 3 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
other-projects/maori-lang-detection/src/org/greenstone/atea/CountryCodeCountsMapData.java
r33790 r33794 14 14 15 15 // For working with GeoJSON's Simple Features in Java 16 import mil.nga.sf.geojson.Feature; 17 import mil.nga.sf.geojson.FeatureCollection; 16 18 import mil.nga.sf.geojson.FeatureConverter; 17 19 import mil.nga.sf.geojson.Geometry; 18 20 import mil.nga.sf.geojson.MultiPoint; 21 import mil.nga.sf.geojson.Polygon; 19 22 import mil.nga.sf.geojson.Position; 20 23 … … 86 89 JsonObject json = obj.getAsJsonObject(); 87 90 String countryCode = json.get("_id").getAsString(); 88 int count = (int)json.get("count").getAsDouble(); 91 int count = (int)json.get("count").getAsDouble(); 89 92 90 93 //logger.info("Got country code: " + countryCode); … … 101 104 Double lat = countryCodeJson.get("latitude").getAsDouble(); 102 105 //logger.info("long: " + Double.toString(lng) + ", lat: " + Double.toString(lat)); 106 String countryName = countryCodeJson.get("name").getAsString(); 103 107 104 108 // 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.html109 json.addProperty("lng", lng); // adds Number: https://javadoc.io/static/com.google.code.gson/gson/2.8.5/com/google/gson/JsonObject.html 106 110 json.addProperty("lat", lat); 111 json.addProperty("region", countryName); 112 107 113 } else { 108 114 logger.info("No geolocation info found for country code " + countryCode); … … 111 117 // add lat and lng for Europe 112 118 json.addProperty("lng", EU_LNG); 113 json.addProperty("lat", EU_LAT); 119 json.addProperty("lat", EU_LAT); 120 json.addProperty("region", "Europe"); 114 121 } 115 122 else if(countryCode.equals("UNKNOWN")) { … … 118 125 json.addProperty("lng", ANTARCTICA_LNG); 119 126 json.addProperty("lat", ANTARCTICA_LAT); 127 json.addProperty("region", "UNKNOWN"); 120 128 } else { 121 129 logger.error("ERROR: entirely unknown country code: " + countryCode); … … 184 192 List<Position> points = new LinkedList<Position>(); 185 193 186 for(JsonElement obj : countryCountsJsonArray) {194 for(JsonElement obj : this.countryCountsJsonArray) { 187 195 JsonObject json = obj.getAsJsonObject(); 188 196 Double lng = json.get("lng").getAsDouble(); … … 198 206 } 199 207 200 208 // https://javadoc.io/static/com.google.code.gson/gson/2.8.5/index.html 209 public FeatureCollection toFeatureCollection() { 210 final int HISTOGRAM_WIDTH = 4; 211 212 FeatureCollection featureCollection = new FeatureCollection(); 213 214 for(JsonElement obj : this.countryCountsJsonArray) { 215 JsonObject json = obj.getAsJsonObject(); 216 217 String countryCode = json.get("_id").getAsString(); 218 String region = json.get("region").getAsString(); 219 int count = json.get("count").getAsInt(); 220 221 // make a histogram for each country 222 Geometry rectangle = this.toPolygon(json, count, HISTOGRAM_WIDTH); 223 224 Feature countryFeature = new Feature(rectangle); 225 Map<String, Object> featureProperties = new HashMap<String, Object>(); 226 featureProperties.put("count", new Integer(count)); 227 featureProperties.put("code", countryCode); 228 featureProperties.put("region", region); 229 countryFeature.setProperties(featureProperties); 230 231 featureCollection.addFeature(countryFeature); 232 } 233 234 return featureCollection; 235 } 236 237 238 // create rectangular "histogram" for each country code 239 private Geometry toPolygon(JsonObject json, int count, int HISTOGRAM_WIDTH) { 240 int half_width = HISTOGRAM_WIDTH/2; 241 double vertical_factor = 1.0; 242 243 Double lng = json.get("lng").getAsDouble(); 244 Double lat = json.get("lat").getAsDouble(); 245 246 String countryCode = json.get("_id").getAsString(); 247 248 // US is a special case. Count is too high by a factor of 8. 249 // So reduce height by twice that factor, and width by twice that factor too. 250 // (A factor of 8 makes north at +125 too high for latitude, 251 // see http://www.learnz.org.nz/sites/learnz.org.nz/files/lat-long-geo-data-01_0.jpg) 252 if(countryCode.equals("US")) { 253 half_width = 16 * half_width; 254 vertical_factor = vertical_factor/16.0; 255 } 256 257 List<List<Position>> outerList = new LinkedList<List<Position>>(); 258 List<Position> points = new LinkedList<Position>(); 259 outerList.add(points); 260 261 //create the 4 corners of the rectangle 262 // West is negative, east is positive, south is negative, north is positive 263 // See http://www.learnz.org.nz/sites/learnz.org.nz/files/lat-long-geo-data-01_0.jpg 264 Double east = lng + half_width; 265 Double west = lng - half_width; 266 Double north = lat + (vertical_factor * count); 267 Double south = lat; 268 points.add(new Position(west, south)); // Position(lng, lat) not Position(lat, lng) 269 points.add(new Position(west, north)); 270 points.add(new Position(east, north)); 271 points.add(new Position(east, south)); 272 273 274 Geometry rectangle = new Polygon(outerList); 275 276 // Coords: a List of List of Positions, see https://ngageoint.github.io/simple-features-geojson-java/docs/api/ 277 // https://www.here.xyz/api/concepts/geojsonbasics/#polygon 278 279 return rectangle; 280 } 281 201 282 public String writeMultiPointGeoJsonToFile(File folder) { 202 283 final String filename = "multipoint.json"; … … 229 310 } 230 311 312 public String writeFeaturesGeoJsonToFile(File folder) { 313 final String filename = "geojson-features.json"; 314 File outFile = new File(folder, filename); 315 316 FeatureCollection featureColl = this.toFeatureCollection(); 317 String featuresGeojsonString = FeatureConverter.toStringValue(featureColl); 318 System.err.println("\nMap data as featurecollection:\n" + featuresGeojsonString + "\n"); 319 try ( 320 Writer writer = new BufferedWriter(new FileWriter(outFile)); 321 ) { 322 323 324 //multiPointGeojsonString = multiPointGeojsonString.replace("[[", "\n[\n\t["); 325 //multiPointGeojsonString = multiPointGeojsonString.replace("],[", "],\n\t["); 326 //multiPointGeojsonString = multiPointGeojsonString.replace("]]", "]\n]"); 327 328 writer.write(featuresGeojsonString + "\n"); 329 } catch(Exception e) { 330 logger.error("Unable to write multipoint geojson:\n**********************"); 331 logger.error(featuresGeojsonString); 332 logger.error("**********************\ninto file " + outFile.getAbsolutePath()); 333 logger.error(e.getMessage(), e); 334 } 335 336 return outFile.getAbsolutePath(); 337 338 } 339 231 340 232 341 // Unfinished and unused … … 259 368 //System.err.println("geometry: " + multiPointGeojsonString); 260 369 261 String outFileName = mapData.writeMultiPointGeoJsonToFile(parentFolder); 262 System.err.println("***********\nWrote mapdata to file " + outFileName); 370 String multipointOutFileName = mapData.writeMultiPointGeoJsonToFile(parentFolder); 371 String featuresOutFileName = mapData.writeFeaturesGeoJsonToFile(parentFolder); 372 System.err.println("***********\nWrote mapdata to files " + multipointOutFileName 373 + " and " + featuresOutFileName); 374 263 375 } catch(Exception e) { 264 376 logger.error(e.getMessage(), e);
Note:
See TracChangeset
for help on using the changeset viewer.