Changeset 33790 for other-projects


Ignore:
Timestamp:
2019-12-10T20:43:53+13:00 (4 years ago)
Author:
ak19
Message:

Got the MultiPoint geojson mapdata of the country code counts working: the geojson mapdata now gets output and can be used at http://geojson.tools. Next time, will work on outputting complex geojson Feature objects: properties about country and count information and counts as histograms made from polygons.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • other-projects/maori-lang-detection/src/org/greenstone/atea/CountryCodeCountsMapData.java

    r33778 r33790  
    1414
    1515// For working with GeoJSON's Simple Features in Java
     16import mil.nga.sf.geojson.FeatureConverter;
    1617import mil.nga.sf.geojson.Geometry;
    1718import mil.nga.sf.geojson.MultiPoint;
     
    1920
    2021
    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 */
    2436
    2537/**
    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.
    2741 *
    2842 * TO COMPILE:
     
    3852    static Logger logger = Logger.getLogger(org.greenstone.atea.CountryCodeCountsMapData.class.getName());
    3953
    40     Map<String, JsonObject> countryToJsonMap;
     54    //Map<String, JsonObject> countryToJsonMap;
    4155    JsonArray countryCodesJsonArray;
    4256    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;
    4364   
    4465    public CountryCodeCountsMapData(String countryCoordsJSONFile, String countryCountsJSONFile) throws Exception {
    4566   
    4667    // 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.
    4972    countryCodesJsonArray = parseJSONFile(countryCoordsJSONFile);
    5073    for(JsonElement obj : countryCodesJsonArray) {
     
    5376    }
    5477
    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.
    5683    countryCountsJsonArray = parseJSONFile(countryCountsJSONFile); 
    5784
     
    6188        int count = (int)json.get("count").getAsDouble();       
    6289       
    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);
    6592
    6693        // locate in countryCode map
    6794        JsonObject countryCodeJson = countryToJsonMap.get(countryCode);
    6895
    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     */
    84131    public JsonArray parseJSONFile(String filename) throws Exception {
    85132    JsonArray jsonArray = null;
     
    100147        // replace last comma with closing bracket
    101148        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       
    104152        // https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java
    105153        jsonArray = new JsonParser().parse(fileContents).getAsJsonArray();   
     
    131179     * has it. So I've been trying to build that, but don't have the correct version of maven.
    132180     */
    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
    138184    List<Position> points = new LinkedList<Position>();
    139185   
    140     //Geometry multipoint = new MultiPoint();
    141186    for(JsonElement obj : countryCountsJsonArray) {
    142187        JsonObject json = obj.getAsJsonObject();
    143         Double lng = json.get("longitude").getAsDouble();
    144         Double lat = json.get("latitude").getAsDouble();
     188        Double lng = json.get("lng").getAsDouble();
     189        Double lat = json.get("lat").getAsDouble();
    145190
    146191        Position point = new Position(lng, lat);
    147192        points.add(point);
    148193    }
    149 
    150     //Geometry multiPoint = new mil.nga.sf.geojson.MultiPoint(points);
     194   
    151195    Geometry multiPoint = new MultiPoint(points);
    152196   
    153197    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
    159233    public void parseCSVFile(String filename) throws Exception {
    160234    File csvData = new File(filename);
    161235    CSVParser parser = CSVParser.parse(csvData, java.nio.charset.Charset.forName("US-ASCII"), CSVFormat.RFC4180);
    162236    for (CSVRecord csvRecord : parser) {
    163         System.err.println("Got record: " + csvRecord.toString());
     237        logger.info("Got record: " + csvRecord.toString());
    164238    }
    165239    }
     
    174248        System.exit(-1);
    175249    }
    176 
     250   
    177251    try {
     252        File countsFile = new File(args[1]);
     253        File parentFolder = countsFile.getParentFile().getCanonicalFile(); // canonical resolves any .. and . in path
     254       
    178255        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);
    180263    } catch(Exception e) {
    181         e.printStackTrace();
     264        logger.error(e.getMessage(), e);
    182265    }
    183266    }
Note: See TracChangeset for help on using the changeset viewer.