View Javadoc
1   package au.gov.amsa.gt;
2   
3   import java.io.InputStreamReader;
4   import java.util.Map;
5   import java.util.stream.Stream;
6   
7   import org.slf4j.Logger;
8   import org.slf4j.LoggerFactory;
9   
10  import au.gov.amsa.streams.Strings;
11  
12  public class Shapes {
13  
14  	private static final Logger log = LoggerFactory.getLogger(Shapes.class);
15  
16  	private final Map<String, Shapefile> shapes;
17  
18  	public Shapes() {
19  		this("/shapes.txt");
20  	}
21  
22  	public Shapes(String resource) {
23  		InputStreamReader r = new InputStreamReader(
24  				Shapes.class.getResourceAsStream(resource));
25  		shapes = Strings
26  				.lines(r)
27  				// ignore comment lines
28  				.filter(line -> !line.startsWith("#"))
29  				// ignore blank lines
30  				.filter(line -> line.trim().length() > 0)
31  				// split by | character
32  				.map(line -> line.split("\\|"))
33  				// log
34  				.doOnNext(items -> {
35  					log.info("loading " + items[0]);
36  				})
37  				// build map
38  				.toMap(items -> items[0].trim(),
39  						items -> shapefileFromZip(items[1].trim(),
40  								Double.parseDouble(items[2].trim())))
41  				// go
42  				.toBlocking().single();
43  	}
44  
45  	private static Shapefile shapefileFromZip(String resource,
46  			double bufferDistance) {
47  		return Shapefile.fromZip(Shapes.class.getResourceAsStream(resource),
48  				bufferDistance);
49  	}
50  
51  	/**
52  	 * Returns the names of those shapes that contain the given lat, lon.
53  	 * 
54  	 * @param lat
55  	 * @param lon
56  	 * @return
57  	 */
58  	public Stream<String> containing(double lat, double lon) {
59  		return shapes.keySet().stream()
60  		// select if contains lat lon
61  				.filter(name -> shapes.get(name).contains(lat, lon));
62  	}
63  
64  	public void add(String name, Shapefile shapefile) {
65  		shapes.put(name, shapefile);
66  	}
67  
68  }