Ignore:
Timestamp:
2020-02-13T22:40:41+13:00 (4 years ago)
Author:
ak19
Message:

SummaryTool now uses the CountryCodeCountsMapData.java class to generate the geojson-features files from the tables it already created using MongoDB query results. Switched over from geojson.tools to geojson.io since the latter allows passing geojson mapdata in the URL. The firefox screenshotting is still not working. But I can't even get complex geojson features to work from the commandline yet, so then there's another possible layer of complexity when running firefox as a Java process. Added jna jar files used by Greenstone's SafeProcess for launching Firefox as a Java process.

File:
1 edited

Legend:

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

    r33869 r33919  
    77import java.io.FileWriter;
    88import java.io.Writer;
     9
     10import java.net.URLEncoder;
    911
    1012import java.util.HashMap;
     
    3335import mil.nga.sf.geojson.Position;
    3436
     37
     38import org.greenstone.util.SafeProcess;
    3539
    3640/**
     
    105109 *   https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations/2.10.0
    106110 */
    107 public class CountryCodeCountsMapData {
     111public class CountryCodeCountsMapData {   
     112   
    108113    static Logger logger = Logger.getLogger(org.greenstone.atea.CountryCodeCountsMapData.class.getName());
     114   
     115    static public final String GEOJSON_MAP_TOOL_URL = "http://geojson.io/"; //"http://geojson.tools/";
     116    static private final String DATA_STR = "#data=data:application/json,";
     117   
     118    // "http://geojson.io" has a URL API to programmatically access
     119    /*
     120       See http://geojson.io/ -> Help
     121
     122       "I'm a coder
     123
     124       geojson.io has an array of cli tools that make it easy to go from a GeoJSON file on your computer to geojson.io."
     125
     126       http://geojson.io/#geojson-io-api
     127       "Geojson.io API
     128
     129       You can interact with geojson.io programmatically in two ways:
     130
     131       => URL parameters
     132       Browser console"
     133
     134       http://geojson.io/#url-api
     135       "data=data:application/json,
     136
     137       Open the map and load a chunk of GeoJSON data from a URL segment directly onto the map.
     138       The GeoJSON data should be encoded as per encodeURIComponent(JSON.stringify(geojson_data)).
     139       Example:
     140
     141       http://geojson.io/#data=data:application/json,%7B%22type%22%3A%22LineString%22%2C%22coordinates%22%3A%5B%5B0%2C0%5D%2C%5B10%2C10%5D%5D%7D
     142    */
     143   
     144    public static final int SUPPRESS_MAPDATA_DISPLAY = 0;
     145    public static final int PRINT_MAPDATA_TO_SCREEN = 1;
    109146
    110147    //Map<String, JsonObject> countryToJsonMap;
     
    121158    private final String geoJsonFilenameWithSuffix;
    122159    private final File outputFolder;
     160
     161
    123162   
    124163    public CountryCodeCountsMapData(String countryCountsJSONFilename) throws Exception {
     
    185224        logger.info("No geolocation info found for country code " + countryCode);
    186225        if(countryCode.equals("EU")) {
     226            logger.info("   Adding lat,lng for somewhere around Europe");
    187227            //logger.info("Unlisted country code: EU");
    188228            // add lat and lng for Europe
     
    192232        }
    193233        else if(countryCode.equals("UNKNOWN")) {
     234            logger.info("   Adding lat,lng for somewhere in Antarctica");
    194235            //logger.info("Unlisted country code: UNKNOWN");
    195236            // add lat and lng for Antarctica
     
    515556    }
    516557   
     558    // by default, display mapdata output on screen too
    517559    public String writeMultiPointGeoJsonToFile() {
     560    return writeMultiPointGeoJsonToFile(PRINT_MAPDATA_TO_SCREEN);
     561    }
     562    public String writeMultiPointGeoJsonToFile(int displayMapData) {
    518563    final String filename = "multipoint_" + this.geoJsonFilenameWithSuffix;
    519564    File outFile = new File(this.outputFolder, filename);
     
    521566    Geometry geometry = this.toMultiPointGeoJson();
    522567    String multiPointGeojsonString = FeatureConverter.toStringValue(geometry);
    523     System.err.println("\nMap data as MultiPoint geometry:\n" + multiPointGeojsonString + "\n");
     568    if(displayMapData == PRINT_MAPDATA_TO_SCREEN) {
     569        System.err.println("\nMap data as MultiPoint geometry:\n" + multiPointGeojsonString + "\n");
     570    }
    524571    try (
    525572         Writer writer = new BufferedWriter(new FileWriter(outFile));
     
    528575        // Some basic re-formatting for some immediate legibility
    529576        // But pasting the contents of the file (or the System.err output above)
    530         // directly into http://geojson.tools/ will instantly reformat the json perfectly anyway.
     577        // directly into http://geojson.tools/ or http://geojson.io/
     578        // will instantly reformat the json perfectly anyway.
    531579        multiPointGeojsonString = multiPointGeojsonString.replace("[[", "\n[\n\t[");
    532580        multiPointGeojsonString = multiPointGeojsonString.replace("],[", "],\n\t[");
     
    544592   
    545593    }
    546    
     594
     595    // by default, display mapdata output on screen too
    547596    public String writeFeaturesGeoJsonToFile() {
     597    return writeFeaturesGeoJsonToFile(PRINT_MAPDATA_TO_SCREEN);
     598    }
     599    // write out geojson features to appropriately named file
     600    // If displayMapData == PRINT_MAPDATA_TO_SCREEN, then it will also be printed to screen
     601    public String writeFeaturesGeoJsonToFile(int displayMapData) {
    548602    final String filename = "geojson-features_" + this.geoJsonFilenameWithSuffix;
    549603    File outFile = new File(this.outputFolder, filename);
     
    551605    FeatureCollection featureColl = this.toFeatureCollection();
    552606    String featuresGeojsonString = FeatureConverter.toStringValue(featureColl);
    553     System.err.println("\nMap data as featurecollection:\n" + featuresGeojsonString + "\n");
     607    if(displayMapData == PRINT_MAPDATA_TO_SCREEN) {
     608        System.err.println("\nMap data as featurecollection:\n" + featuresGeojsonString + "\n");
     609    }
    554610    try (
    555611         Writer writer = new BufferedWriter(new FileWriter(outFile));
     
    564620    }
    565621
    566     return outFile.getAbsolutePath();
    567    
    568     }
    569 
     622    return outFile.getAbsolutePath();   
     623    }
     624   
     625
     626    public String getFeaturesGeoJsonString(boolean uriEncoded) {
     627    String featuresGeojsonString = FeatureConverter.toStringValue(this.toFeatureCollection());
     628    if(uriEncoded) {
     629        // Want to return encodeURIComponent(JSON.stringify(featuresGeojsonString));
     630        // https://stackoverflow.com/questions/607176/java-equivalent-to-javascripts-encodeuricomponent-that-produces-identical-outpu
     631        URLEncoder.encode(featuresGeojsonString);
     632    }
     633    return featuresGeojsonString;
     634    }
     635
     636    public String getAsMapURL() {
     637    boolean uriEncoded = true;
     638    String url = GEOJSON_MAP_TOOL_URL + DATA_STR + getFeaturesGeoJsonString(uriEncoded);
     639
     640    return url;
     641    }
     642
     643    public String geoJsonMapScreenshot(File outputFolder, String fileNamePrefix) {
     644    // https://stackoverflow.com/questions/49606051/how-to-take-a-screenshot-in-firefox-headless-selenium-in-java
     645   
     646    // https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Headless_mode
     647    // /path/to/firefox -P my-profile --screenshot test.jpg  https://developer.mozilla.org --window-size=800,1000
     648    // https://stackoverflow.com/questions/15783701/which-characters-need-to-be-escaped-when-using-bash
     649   
     650    String mapURL = this.getAsMapURL();
     651
     652    String mapURLescapedForBash = mapURL.replace("\"", "\\\"");//.replace("[", "\\[").replace("]", "\\]");
     653   
     654    File outputFile = new File(outputFolder + File.separator + fileNamePrefix+".png");
     655    String outputFilePath = Utility.getFilePath(outputFile);
     656
     657   
     658    String[] cmdArgs = {
     659        "firefox",
     660        "--screenshot",
     661        outputFilePath,
     662        mapURLescapedForBash //"'" + mapURL + "'"
     663    };
     664   
     665    System.err.print("Running:");
     666    for(String arg : cmdArgs) {
     667        System.err.print(" " + arg);
     668    }
     669    System.err.println();
     670   
     671
     672    //String cmdArgs = "firefox --screenshot " + outputFilePath + " " + GEOJSON_MAP_TOOL_URL + DATA_STR;
     673    //String cmdArgs = "firefox --screenshot " + outputFilePath + " " + "'" + mapURL + "'";
     674    //System.err.println("Running: " + cmdArgs);
     675   
     676    SafeProcess proc = new SafeProcess(cmdArgs);
     677
     678    int retVal = proc.runProcess();
     679
     680    logger.info("Process out: " + proc.getStdOutput());
     681    logger.info("Process err: " + proc.getStdError());
     682    logger.info("Screenshot process returned with: " + retVal);
     683   
     684    return outputFilePath;
     685   
     686    }
    570687   
    571688    public int getTotalCount() {
     
    613730        System.err.println("***********\nWrote mapdata to files " + multipointOutFileName
    614731                   + " and " + featuresOutFileName);
    615         System.err.println("You can paste the geojson contents of either of these files into the "
    616                    + "editor at http://geojson.tools/ to see the data arranged on a world map");
     732        System.err.println("You can paste the geojson contents of either of these files into "
     733                   + " the editor at " + GEOJSON_MAP_TOOL_URL
     734                   + " to see the data arranged on a world map");
    617735
    618736        System.err.println("Total count for query: " + mapData.getTotalCount());
Note: See TracChangeset for help on using the changeset viewer.