Skip to content

Commit

Permalink
Use polygon-clipping for dissolve
Browse files Browse the repository at this point in the history
  • Loading branch information
brendannee committed Nov 20, 2020
1 parent 20cf16c commit ed538ac
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 342 deletions.
25 changes: 20 additions & 5 deletions lib/formats/lines-dissolved.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const gtfs = require('gtfs');
const buffer = require('@turf/buffer');
const dissolve = require('@turf/dissolve').default;
const { geomEach } = require('@turf/meta');
const { multiPolygon } = require('@turf/helpers');
const polygonClipping = require('polygon-clipping');

const { simplifyGeoJSON, stripNonAgencyProperties } = require('../geojson-utils');
const { simplifyGeoJSON } = require('../geojson-utils');

module.exports = async (config, routeId, directionId) => {
const query = {};
Expand All @@ -15,8 +17,21 @@ module.exports = async (config, routeId, directionId) => {
const lines = await gtfs.getShapesAsGeoJSON(query);
const bufferedLines = buffer(lines, config.bufferSizeMeters, { units: 'meters' });

// Need to simplify geojson before dissolving otherwise turf.dissolve can hang.
const dissolved = dissolve(simplifyGeoJSON(bufferedLines, config));
const geometries = [];
geomEach(bufferedLines, (geometry) => {
if (geometry.type === 'MultiPolygon') {
geometries.push(geometry.coordinates);
}
if (geometry.type === 'Polygon') {
geometries.push([geometry.coordinates]);
}
});

return stripNonAgencyProperties(dissolved);
const unioned = polygonClipping.union(...geometries);
const geojson = multiPolygon(unioned);

// Assign agency_name
geojson.properties.agency_name = bufferedLines.features[0].properties.agency_name;

return simplifyGeoJSON(geojson, config);
};
27 changes: 20 additions & 7 deletions lib/formats/stops-dissolved.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const gtfs = require('gtfs');
const buffer = require('@turf/buffer');
const dissolve = require('@turf/dissolve').default;
const { geomEach } = require('@turf/meta');
const { multiPolygon } = require('@turf/helpers');
const polygonClipping = require('polygon-clipping');

const { simplifyGeoJSON, stripNonAgencyProperties } = require('../geojson-utils');
const { simplifyGeoJSON } = require('../geojson-utils');

module.exports = async (config, routeId, directionId) => {
const query = {};
Expand All @@ -16,10 +18,21 @@ module.exports = async (config, routeId, directionId) => {

const bufferedStops = buffer(stops, config.bufferSizeMeters, { units: 'meters' });

// Run dissolve twice to handle dissolves missed on the first pass.
// Need to simplify geojson before dissolving otherwise turf.dissolve can hang.
const dissolved = dissolve(simplifyGeoJSON(bufferedStops, config));
const dissolved2 = dissolve(dissolved);
const geometries = [];
geomEach(bufferedStops, (geometry) => {
if (geometry.type === 'MultiPolygon') {
geometries.push(geometry.coordinates);
}
if (geometry.type === 'Polygon') {
geometries.push([geometry.coordinates]);
}
});

return stripNonAgencyProperties(dissolved2);
const unioned = polygonClipping.union(...geometries);
const geojson = multiPolygon(unioned);

// Assign agency_name
geojson.properties.agency_name = bufferedStops.features[0].properties.agency_name;

return simplifyGeoJSON(geojson, config);
};
Loading

0 comments on commit ed538ac

Please sign in to comment.