Changeset 33794 for other-projects


Ignore:
Timestamp:
2019-12-11T21:57:02+13:00 (4 years ago)
Author:
ak19
Message:

Wrote the geojson map data created from the site counts per country/region as a geojson FeaturesCollection with histogram rectangles representing counts for each country/region. Also adding a screenshot of the map produced from this using http://geojson.tools/

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  
    1414
    1515// For working with GeoJSON's Simple Features in Java
     16import mil.nga.sf.geojson.Feature;
     17import mil.nga.sf.geojson.FeatureCollection;
    1618import mil.nga.sf.geojson.FeatureConverter;
    1719import mil.nga.sf.geojson.Geometry;
    1820import mil.nga.sf.geojson.MultiPoint;
     21import mil.nga.sf.geojson.Polygon;
    1922import mil.nga.sf.geojson.Position;
    2023
     
    8689        JsonObject json = obj.getAsJsonObject();
    8790        String countryCode = json.get("_id").getAsString();
    88         int count = (int)json.get("count").getAsDouble();       
     91        int count = (int)json.get("count").getAsDouble();
    8992       
    9093        //logger.info("Got country code: " + countryCode);
     
    101104        Double lat = countryCodeJson.get("latitude").getAsDouble();
    102105        //logger.info("long: " + Double.toString(lng) + ", lat: " + Double.toString(lat));
     106        String countryName = countryCodeJson.get("name").getAsString();
    103107       
    104108        // 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
     109        json.addProperty("lng", lng); // adds Number: https://javadoc.io/static/com.google.code.gson/gson/2.8.5/com/google/gson/JsonObject.html
    106110        json.addProperty("lat", lat);
     111        json.addProperty("region", countryName);
     112       
    107113        } else {
    108114        logger.info("No geolocation info found for country code " + countryCode);
     
    111117            // add lat and lng for Europe
    112118            json.addProperty("lng", EU_LNG);
    113             json.addProperty("lat", EU_LAT);           
     119            json.addProperty("lat", EU_LAT);
     120            json.addProperty("region", "Europe");
    114121        }
    115122        else if(countryCode.equals("UNKNOWN")) {
     
    118125            json.addProperty("lng", ANTARCTICA_LNG);
    119126            json.addProperty("lat", ANTARCTICA_LAT);
     127            json.addProperty("region", "UNKNOWN");
    120128        } else {
    121129            logger.error("ERROR: entirely unknown country code: " + countryCode);
     
    184192    List<Position> points = new LinkedList<Position>();
    185193   
    186     for(JsonElement obj : countryCountsJsonArray) {
     194    for(JsonElement obj : this.countryCountsJsonArray) {
    187195        JsonObject json = obj.getAsJsonObject();
    188196        Double lng = json.get("lng").getAsDouble();
     
    198206    }
    199207
    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   
    201282    public String writeMultiPointGeoJsonToFile(File folder) {
    202283    final String filename = "multipoint.json";
     
    229310    }
    230311   
     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
    231340   
    232341    // Unfinished and unused
     
    259368        //System.err.println("geometry: " + multiPointGeojsonString);
    260369
    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
    263375    } catch(Exception e) {
    264376        logger.error(e.getMessage(), e);
Note: See TracChangeset for help on using the changeset viewer.