Changeset 33919 for other-projects/maori-lang-detection/src/org/greenstone/atea/CountryCodeCountsMapData.java
- Timestamp:
- 2020-02-13T22:40:41+13:00 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
other-projects/maori-lang-detection/src/org/greenstone/atea/CountryCodeCountsMapData.java
r33869 r33919 7 7 import java.io.FileWriter; 8 8 import java.io.Writer; 9 10 import java.net.URLEncoder; 9 11 10 12 import java.util.HashMap; … … 33 35 import mil.nga.sf.geojson.Position; 34 36 37 38 import org.greenstone.util.SafeProcess; 35 39 36 40 /** … … 105 109 * https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations/2.10.0 106 110 */ 107 public class CountryCodeCountsMapData { 111 public class CountryCodeCountsMapData { 112 108 113 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; 109 146 110 147 //Map<String, JsonObject> countryToJsonMap; … … 121 158 private final String geoJsonFilenameWithSuffix; 122 159 private final File outputFolder; 160 161 123 162 124 163 public CountryCodeCountsMapData(String countryCountsJSONFilename) throws Exception { … … 185 224 logger.info("No geolocation info found for country code " + countryCode); 186 225 if(countryCode.equals("EU")) { 226 logger.info(" Adding lat,lng for somewhere around Europe"); 187 227 //logger.info("Unlisted country code: EU"); 188 228 // add lat and lng for Europe … … 192 232 } 193 233 else if(countryCode.equals("UNKNOWN")) { 234 logger.info(" Adding lat,lng for somewhere in Antarctica"); 194 235 //logger.info("Unlisted country code: UNKNOWN"); 195 236 // add lat and lng for Antarctica … … 515 556 } 516 557 558 // by default, display mapdata output on screen too 517 559 public String writeMultiPointGeoJsonToFile() { 560 return writeMultiPointGeoJsonToFile(PRINT_MAPDATA_TO_SCREEN); 561 } 562 public String writeMultiPointGeoJsonToFile(int displayMapData) { 518 563 final String filename = "multipoint_" + this.geoJsonFilenameWithSuffix; 519 564 File outFile = new File(this.outputFolder, filename); … … 521 566 Geometry geometry = this.toMultiPointGeoJson(); 522 567 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 } 524 571 try ( 525 572 Writer writer = new BufferedWriter(new FileWriter(outFile)); … … 528 575 // Some basic re-formatting for some immediate legibility 529 576 // 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. 531 579 multiPointGeojsonString = multiPointGeojsonString.replace("[[", "\n[\n\t["); 532 580 multiPointGeojsonString = multiPointGeojsonString.replace("],[", "],\n\t["); … … 544 592 545 593 } 546 594 595 // by default, display mapdata output on screen too 547 596 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) { 548 602 final String filename = "geojson-features_" + this.geoJsonFilenameWithSuffix; 549 603 File outFile = new File(this.outputFolder, filename); … … 551 605 FeatureCollection featureColl = this.toFeatureCollection(); 552 606 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 } 554 610 try ( 555 611 Writer writer = new BufferedWriter(new FileWriter(outFile)); … … 564 620 } 565 621 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 } 570 687 571 688 public int getTotalCount() { … … 613 730 System.err.println("***********\nWrote mapdata to files " + multipointOutFileName 614 731 + " 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"); 617 735 618 736 System.err.println("Total count for query: " + mapData.getTotalCount());
Note:
See TracChangeset
for help on using the changeset viewer.