|
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;
|
@@ -734,6 +736,70 @@ public static List<Grid> fromShapefile (File shapefile, int zoom, ProgressListen
|
734 | 736 | return new ArrayList<>(grids.values());
|
735 | 737 | }
|
736 | 738 |
|
| 739 | + /** |
| 740 | + * Take an `InputStream` containing GeoJson Features and turn it into an opportunity grid. |
| 741 | + */ |
| 742 | + public static List<Grid> fromGeoJson (InputStream geoJsonInputStream, int zoom, ProgressListener progressListener) |
| 743 | + throws IOException { |
| 744 | + GeoJSONReader reader = new GeoJSONReader(geoJsonInputStream); |
| 745 | + SimpleFeatureCollection features = reader.getFeatures(); |
| 746 | + Envelope envelope = features.getBounds(); |
| 747 | + |
| 748 | + checkWgsEnvelopeSize(envelope, "Shapefile"); |
| 749 | + WebMercatorExtents extents = WebMercatorExtents.forWgsEnvelope(envelope, zoom); |
| 750 | + |
| 751 | + int total = features.size(); |
| 752 | + if (progressListener != null) { |
| 753 | + progressListener.setTotalItems(total); |
| 754 | + } |
| 755 | + |
| 756 | + AtomicInteger count = new AtomicInteger(0); |
| 757 | + HashMap<String, Grid> grids = new HashMap<>(); |
| 758 | + |
| 759 | + SimpleFeatureIterator featureIterator = features.features(); |
| 760 | + while (featureIterator.hasNext()) { |
| 761 | + SimpleFeature feature = featureIterator.next(); |
| 762 | + Geometry geom = (Geometry) feature.getDefaultGeometry(); |
| 763 | + |
| 764 | + for (var p : feature.getProperties()) { |
| 765 | + var val = p.getValue(); |
| 766 | + |
| 767 | + if (!(val instanceof Number)) continue; |
| 768 | + double numericVal = ((Number) val).doubleValue(); |
| 769 | + if (numericVal == 0) continue; |
| 770 | + |
| 771 | + String attributeName = p.getName().getLocalPart(); |
| 772 | + |
| 773 | + Grid grid = grids.get(attributeName); |
| 774 | + if (grid == null) { |
| 775 | + grid = new Grid(extents); |
| 776 | + grid.name = attributeName; |
| 777 | + grids.put(attributeName, grid); |
| 778 | + } |
| 779 | + |
| 780 | + if (geom instanceof Point) { |
| 781 | + Point point = (Point) geom; |
| 782 | + // already in WGS 84 |
| 783 | + grid.incrementPoint(point.getY(), point.getX(), numericVal); |
| 784 | + } else if (geom instanceof Polygon || geom instanceof MultiPolygon) { |
| 785 | + grid.rasterize(geom, numericVal); |
| 786 | + } else { |
| 787 | + throw new IllegalArgumentException("Unsupported geometry type: " + geom); |
| 788 | + } |
| 789 | + } |
| 790 | + |
| 791 | + int currentCount = count.incrementAndGet(); |
| 792 | + if (progressListener != null) { |
| 793 | + progressListener.setCompletedItems(currentCount); |
| 794 | + } |
| 795 | + if (currentCount % 10000 == 0) { |
| 796 | + LOG.info("{} / {} features read", human(currentCount), human(total)); |
| 797 | + } |
| 798 | + } |
| 799 | + reader.close(); |
| 800 | + return new ArrayList<>(grids.values()); |
| 801 | + } |
| 802 | + |
737 | 803 | @Override
|
738 | 804 | public double sumTotalOpportunities() {
|
739 | 805 | double totalOpportunities = 0;
|
|
0 commit comments