|
17 | 17 | import org.geotools.data.FileDataStore; |
18 | 18 | import org.geotools.data.FileDataStoreFinder; |
19 | 19 | import org.geotools.data.Transaction; |
| 20 | +import org.geotools.data.geojson.GeoJSONReader; |
| 21 | +import org.geotools.data.simple.SimpleFeatureCollection; |
| 22 | +import org.geotools.data.simple.SimpleFeatureIterator; |
20 | 23 | import org.geotools.feature.simple.SimpleFeatureTypeBuilder; |
21 | 24 | import org.geotools.gce.geotiff.GeoTiffFormat; |
22 | 25 | import org.geotools.gce.geotiff.GeoTiffWriteParams; |
|
64 | 67 |
|
65 | 68 | import static com.conveyal.gtfs.util.Util.human; |
66 | 69 | import static com.conveyal.r5.common.GeometryUtils.checkWgsEnvelopeSize; |
67 | | -import static com.google.common.base.Preconditions.checkArgument; |
68 | 70 | import static com.google.common.base.Preconditions.checkState; |
69 | 71 | import static java.lang.Double.parseDouble; |
70 | 72 | import static org.apache.commons.math3.util.FastMath.atan; |
@@ -748,6 +750,70 @@ public static List<Grid> fromShapefile (File shapefile, int zoom, ProgressListen |
748 | 750 | return new ArrayList<>(grids.values()); |
749 | 751 | } |
750 | 752 |
|
| 753 | + /** |
| 754 | + * Take an `InputStream` containing GeoJson Features and turn it into an opportunity grid. |
| 755 | + */ |
| 756 | + public static List<Grid> fromGeoJson (InputStream geoJsonInputStream, int zoom, ProgressListener progressListener) |
| 757 | + throws IOException { |
| 758 | + GeoJSONReader reader = new GeoJSONReader(geoJsonInputStream); |
| 759 | + SimpleFeatureCollection features = reader.getFeatures(); |
| 760 | + Envelope envelope = features.getBounds(); |
| 761 | + |
| 762 | + checkWgsEnvelopeSize(envelope, "Shapefile"); |
| 763 | + WebMercatorExtents extents = WebMercatorExtents.forWgsEnvelope(envelope, zoom); |
| 764 | + |
| 765 | + int total = features.size(); |
| 766 | + if (progressListener != null) { |
| 767 | + progressListener.setTotalItems(total); |
| 768 | + } |
| 769 | + |
| 770 | + AtomicInteger count = new AtomicInteger(0); |
| 771 | + HashMap<String, Grid> grids = new HashMap<>(); |
| 772 | + |
| 773 | + SimpleFeatureIterator featureIterator = features.features(); |
| 774 | + while (featureIterator.hasNext()) { |
| 775 | + SimpleFeature feature = featureIterator.next(); |
| 776 | + Geometry geom = (Geometry) feature.getDefaultGeometry(); |
| 777 | + |
| 778 | + for (var p : feature.getProperties()) { |
| 779 | + var val = p.getValue(); |
| 780 | + |
| 781 | + if (!(val instanceof Number)) continue; |
| 782 | + double numericVal = ((Number) val).doubleValue(); |
| 783 | + if (numericVal == 0) continue; |
| 784 | + |
| 785 | + String attributeName = p.getName().getLocalPart(); |
| 786 | + |
| 787 | + Grid grid = grids.get(attributeName); |
| 788 | + if (grid == null) { |
| 789 | + grid = new Grid(extents); |
| 790 | + grid.name = attributeName; |
| 791 | + grids.put(attributeName, grid); |
| 792 | + } |
| 793 | + |
| 794 | + if (geom instanceof Point) { |
| 795 | + Point point = (Point) geom; |
| 796 | + // already in WGS 84 |
| 797 | + grid.incrementPoint(point.getY(), point.getX(), numericVal); |
| 798 | + } else if (geom instanceof Polygon || geom instanceof MultiPolygon) { |
| 799 | + grid.rasterize(geom, numericVal); |
| 800 | + } else { |
| 801 | + throw new IllegalArgumentException("Unsupported geometry type: " + geom); |
| 802 | + } |
| 803 | + } |
| 804 | + |
| 805 | + int currentCount = count.incrementAndGet(); |
| 806 | + if (progressListener != null) { |
| 807 | + progressListener.setCompletedItems(currentCount); |
| 808 | + } |
| 809 | + if (currentCount % 10000 == 0) { |
| 810 | + LOG.info("{} / {} features read", human(currentCount), human(total)); |
| 811 | + } |
| 812 | + } |
| 813 | + reader.close(); |
| 814 | + return new ArrayList<>(grids.values()); |
| 815 | + } |
| 816 | + |
751 | 817 | @Override |
752 | 818 | public double sumTotalOpportunities() { |
753 | 819 | double totalOpportunities = 0; |
|
0 commit comments