Changeset 33867

Show
Ignore:
Timestamp:
23.01.2020 21:12:17 (4 weeks ago)
Author:
ak19
Message:

Moved the code handling of special case large rectangles and those that exceed one or more of the geojson.tools horizontal wrap point (at -40 longitude) into a new function called recalculateAreaIfLarge(...)

Files:
1 modified

Legend:

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

    r33858 r33867  
    358358    Double west = lng - half_width; 
    359359    Double south = lat; 
     360             
     361    List<Position> pts = recalculateAreaIfLarge(count, HISTOGRAM_WIDTH, countryCode, lat, lng, north, south, east, west); 
     362 
    360363    /* 
    361364    System.err.println("For country " + countryCode + ":"); 
     
    364367    System.err.println("east = " + east); 
    365368    System.err.println("west = " + west + "\n"); 
     369    System.err.println("-------------"); 
    366370    */ 
     371         
     372    List<List<Position>> outerList = new LinkedList<List<Position>>(); 
     373    if(pts != null) { 
     374        outerList.add(pts); 
     375    } else { 
     376         
     377         
     378        List<Position> points = new LinkedList<Position>();  
     379        outerList.add(points);   
     380     
     381        points.add(new Position(west, south)); // Position(lng, lat) not Position(lat, lng) 
     382        points.add(new Position(west, north)); 
     383        points.add(new Position(east, north)); 
     384        points.add(new Position(east, south)); 
     385    } 
     386 
     387    Geometry rectangle = new Polygon(outerList); 
     388     
     389    // Coords: a List of List of Positions, see https://ngageoint.github.io/simple-features-geojson-java/docs/api/ 
     390    // https://www.here.xyz/api/concepts/geojsonbasics/#polygon 
     391 
     392    return rectangle; 
     393    } 
     394 
     395    private List<Position> recalculateAreaIfLarge(final int count, final int HISTOGRAM_WIDTH, String countryCode, 
     396                    final Double lat, final Double lng, 
     397                    Double north, Double south, Double east, Double west) { 
     398    boolean recalculated = false; 
     399     
    367400    // Check if we're dealing with very large numbers, in which case, we can have follow off the longitude edges 
    368401    // Max longitude values are -180 to 180. So a max of 360 units between them. (Max latitude is -90 to 90) 
     
    371404    // and the South pole has a latitude of -90°." 
    372405    if((east + Math.abs(west)) > 360 || east > 180 || west < -180) { 
    373         half_width = HISTOGRAM_WIDTH/2; // reset half_width 
     406        System.err.println("For country " + countryCode + ":"); 
     407        System.err.println("north = " + north); 
     408        System.err.println("south = " + south); 
     409        System.err.println("east = " + east); 
     410        System.err.println("west = " + west + "\n"); 
     411 
     412        int half_width = HISTOGRAM_WIDTH/2; // reset half_width 
    374413         
    375414        double v_tmp_count = Math.sqrt(count); 
     
    377416        //v_tmp_count = Math.ceil(v_tmp_count); 
    378417        double h_tmp_count = v_tmp_count; 
    379  
    380         /* 
     418System.err.println("count = " + count); 
     419        System.err.println("v = " + v_tmp_count); 
     420        System.err.println("h = " + h_tmp_count); 
     421        System.err.println("lat = " + lat); 
     422        System.err.println("lng = " + lng + "\n"); 
     423         
     424        if(h_tmp_count > 90) { // 360 max width, of which each longitude 
     425        // is 4 units (horizontal factor = 4, and half-width is half 
     426        // of that). So max width/h_tmp_count allowed 90 => 360 
     427        // longitude on map (-180 to 180). 
     428        // Put the excess h_tmp_count into v_tmp_count and ensure 
     429        // that does not go over 90+90 = 180 max. Vertical_factor is 1. 
     430 
     431        System.err.println("Out of longitude range. Attempting to compensate..."); 
     432         
     433        double diff = h_tmp_count - 80.0; // actually 90 wraps on geojson tools, 80 doesn't 
     434        h_tmp_count -= diff; 
     435        v_tmp_count = (count/h_tmp_count); 
     436         
     437        if(v_tmp_count > 180 || h_tmp_count > 90) { 
     438            System.err.println("Warning: still exceeded max latitude and/or longitude range");           
     439        } 
     440         
     441        } 
     442         
    381443        System.err.println("Recalculating polygon for country with high count: " + countryCode + "."); 
    382444        System.err.println("count = " + count); 
     
    385447        System.err.println("lat = " + lat); 
    386448        System.err.println("lng = " + lng + "\n");       
    387         */ 
     449         
    388450         
    389451        north = lat + v_tmp_count; 
     
    419481         
    420482        // Hopefully we don't exceed +90/-90 lat and +/-180 longitude 
    421         /* 
    422         System.err.println("north = " + north); 
     483 
     484        recalculated = true; 
     485 
     486         
     487    } else if(west < -140.0) { 
     488        // past -140 west, the edges don't wrap well in geotools, so shift any points more west/negative than -140: 
     489         
     490        double diff = -140.0 - west; 
     491        west = -140.0; 
     492        east += diff; 
     493 
     494        recalculated = true; 
     495    } 
     496 
     497    if(recalculated) { 
     498        System.err.println("\nnorth = " + north); 
    423499        System.err.println("south = " + south); 
    424500        System.err.println("east = " + east); 
    425501        System.err.println("west = " + west); 
    426         */ 
    427     } 
    428      
    429     List<List<Position>> outerList = new LinkedList<List<Position>>(); 
    430     List<Position> points = new LinkedList<Position>();  
    431     outerList.add(points); 
    432  
    433      
    434     points.add(new Position(west, south)); // Position(lng, lat) not Position(lat, lng) 
    435     points.add(new Position(west, north)); 
    436     points.add(new Position(east, north)); 
    437     points.add(new Position(east, south)); 
    438  
    439  
    440     Geometry rectangle = new Polygon(outerList); 
    441      
    442     // Coords: a List of List of Positions, see https://ngageoint.github.io/simple-features-geojson-java/docs/api/ 
    443     // https://www.here.xyz/api/concepts/geojsonbasics/#polygon 
    444  
    445     return rectangle; 
     502 
     503         
     504        List<Position> points = new LinkedList<Position>();  
     505     
     506        points.add(new Position(west, south)); // Position(lng, lat) not Position(lat, lng) 
     507        points.add(new Position(west, north)); 
     508        points.add(new Position(east, north)); 
     509        points.add(new Position(east, south)); 
     510 
     511        return points; 
     512    } 
     513 
     514    return null; 
    446515    } 
    447516