diff --git a/packages/turf-buffer/index.js b/packages/turf-buffer/index.js index 28665127b..941fb0f87 100644 --- a/packages/turf-buffer/index.js +++ b/packages/turf-buffer/index.js @@ -3,6 +3,10 @@ var jsts = require('jsts'); var meta = require('@turf/meta'); var center = require('@turf/center'); var helpers = require('@turf/helpers'); +// var projection = require('@turf/projection'); +var projection = require('./../turf-projection'); +var toMercator = projection.toMercator; +var toWgs84 = projection.toWgs84; var feature = helpers.feature; var geomEach = meta.geomEach; var featureEach = meta.featureEach; @@ -95,25 +99,39 @@ function buffer(geojson, radius, units, steps) { // Project GeoJSON to Transverse Mercator projection (convert to Meters) var distance = radiansToDistance(distanceToRadians(radius, units), 'meters'); - var projection = defineProjection(geojson); - var projected = { - type: geometry.type, - coordinates: projectCoords(geometry.coordinates, projection) - }; + // var projection = defineProjection(geojson); + // var projected = { + // type: geometry.type, + // coordinates: projectCoords(geometry.coordinates, projection) + // }; + + var mercator = toMercator(geojson); + // JSTS buffer operation + var reader1 = new jsts.io.GeoJSONReader(); + var geom1 = reader1.read(mercator.geometry || mercator); + var buffered1 = geom1.buffer(distance); + var writer1 = new jsts.io.GeoJSONWriter(); + buffered1 = writer1.write(buffered1); + // JSTS buffer operation - var reader = new jsts.io.GeoJSONReader(); - var geom = reader.read(projected); - var buffered = geom.buffer(distance); - var writer = new jsts.io.GeoJSONWriter(); - buffered = writer.write(buffered); + // var reader = new jsts.io.GeoJSONReader(); + // var geom = reader.read(projected); + // var buffered = geom.buffer(distance); + // var writer = new jsts.io.GeoJSONWriter(); + // buffered = writer.write(buffered); // Detect if empty geometries - if (coordsIsNaN(buffered.coordinates)) return undefined; + // if (coordsIsNaN(buffered.coordinates)) return undefined; + if (coordsIsNaN(buffered1.coordinates)) return undefined; // Unproject coordinates (convert to Degrees) - buffered.coordinates = unprojectCoords(buffered.coordinates, projection); - return feature(buffered, properties); + // buffered.coordinates = unprojectCoords(buffered.coordinates, projection); + + var result = toWgs84(buffered1); + // result.properties = properties; + return (result.geometry) ? result : feature(result, properties); + // return feature(buffered, properties); } /** diff --git a/packages/turf-buffer/package.json b/packages/turf-buffer/package.json index 5b256422f..d74d410a9 100644 --- a/packages/turf-buffer/package.json +++ b/packages/turf-buffer/package.json @@ -45,6 +45,7 @@ "dependencies": { "@turf/center": "^4.6.1", "@turf/helpers": "^4.6.0", + "@turf/projection": "^4.6.0", "@turf/meta": "^4.6.0", "d3-geo": "^1.6.3", "jsts": "1.3.0" diff --git a/packages/turf-buffer/test/in/issue-#900.geojson b/packages/turf-buffer/test/in/issue-#900.geojson new file mode 100644 index 000000000..cdae6fc3f --- /dev/null +++ b/packages/turf-buffer/test/in/issue-#900.geojson @@ -0,0 +1,273 @@ +{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -86.85, + 21.025 + ], + [ + -86.53333, + 22.39167 + ], + [ + -86.45, + 22.765 + ], + [ + -86.35, + 23.21 + ], + [ + -86.15, + 24.04 + ], + [ + -85.33333, + 25.34167 + ], + [ + -85.26667, + 26.03 + ], + [ + -85.23333, + 26.38333 + ], + [ + -85.18333, + 26.5 + ], + [ + -84.78333, + 27.49333 + ], + [ + -83.41667, + 28 + ], + [ + -83.21667, + 28.175 + ], + [ + -82.63333, + 28.69 + ], + [ + -82.6, + 29.43 + ], + [ + -82.55, + 30.505 + ], + [ + -81.18333, + 32.14667 + ], + [ + -79.65, + 34.23333 + ], + [ + -79.48333, + 34.54333 + ], + [ + -78.93333, + 35.56667 + ], + [ + -78.78333, + 35.87333 + ], + [ + -78.53333, + 36.15833 + ], + [ + -78.16667, + 36.565 + ], + [ + -77.9, + 36.86167 + ], + [ + -77.31667, + 37.50167 + ], + [ + -76.83333, + 37.97 + ], + [ + -76.65, + 38.1 + ], + [ + -76.4, + 38.28833 + ], + [ + -76.01667, + 38.695 + ], + [ + -75.91667, + 38.80333 + ], + [ + -75.78333, + 38.94333 + ], + [ + -75.66667, + 39.06167 + ], + [ + -75.6, + 39.13833 + ], + [ + -75.51667, + 39.23167 + ], + [ + -74.41667, + 39.81667 + ], + [ + -73.7, + 40.205 + ], + [ + -72.31667, + 40.91833 + ], + [ + -71.56667, + 41.16667 + ], + [ + -70.01667, + 41.28167 + ], + [ + -67, + 42.63333 + ], + [ + -56.46667, + 45.60333 + ], + [ + -52, + 46.5 + ], + [ + -50.45, + 46.89 + ], + [ + -50, + 47 + ], + [ + -42.86667, + 47.805 + ], + [ + -40, + 48 + ], + [ + -38.03333, + 48.48 + ], + [ + -30, + 50 + ], + [ + -27.85, + 50.29 + ], + [ + -20, + 51 + ], + [ + -19.33333, + 51.01167 + ], + [ + -15, + 51 + ], + [ + -14, + 51 + ], + [ + -8.5, + 51 + ], + [ + -8.25, + 51 + ], + [ + -8, + 51 + ], + [ + -5.88333, + 50.935 + ], + [ + -3.98333, + 50.84333 + ], + [ + -2.65, + 50.76 + ], + [ + -2.51667, + 50.75 + ], + [ + -2.11667, + 50.675 + ], + [ + -1.6, + 50.575 + ], + [ + -0.93333, + 50.82 + ], + [ + -0.75, + 50.855 + ], + [ + -0.08333, + 50.88667 + ], + [ + -0.18333, + 50.985 + ], + [ + -0.18333, + 51.14833 + ] + ] + } +} \ No newline at end of file diff --git a/packages/turf-buffer/test/out/issue-#900.geojson b/packages/turf-buffer/test/out/issue-#900.geojson new file mode 100644 index 000000000..31fc2b5c3 --- /dev/null +++ b/packages/turf-buffer/test/out/issue-#900.geojson @@ -0,0 +1,1247 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#F00", + "fill": "#F00", + "marker-color": "#F00", + "fill-opacity": 0.3 + }, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + -87.240659, + 22.529386 + ], + [ + -87.157969, + 22.899479 + ], + [ + -87.057861, + 23.344518 + ], + [ + -87.055853, + 23.353139 + ], + [ + -86.855853, + 24.182233 + ], + [ + -86.824535, + 24.277074 + ], + [ + -86.777988, + 24.366501 + ], + [ + -86.039684, + 25.540779 + ], + [ + -85.986863, + 26.085619 + ], + [ + -85.953602, + 26.437944 + ], + [ + -85.908199, + 26.615088 + ], + [ + -85.861035, + 26.72492 + ], + [ + -85.463709, + 27.709667 + ], + [ + -85.39886, + 27.829004 + ], + [ + -85.307628, + 27.93386 + ], + [ + -85.193923, + 28.019788 + ], + [ + -85.06262, + 28.083152 + ], + [ + -83.825769, + 28.539299 + ], + [ + -83.727256, + 28.625098 + ], + [ + -83.344523, + 28.961834 + ], + [ + -83.322289, + 29.454764 + ], + [ + -83.272263, + 30.53007 + ], + [ + -83.253334, + 30.648629 + ], + [ + -83.208391, + 30.761741 + ], + [ + -83.139096, + 30.865246 + ], + [ + -81.78643, + 32.484045 + ], + [ + -80.291108, + 34.511808 + ], + [ + -80.144523, + 34.783603 + ], + [ + -79.600281, + 35.793326 + ], + [ + -79.455143, + 36.08922 + ], + [ + -79.372862, + 36.211546 + ], + [ + -79.122862, + 36.495321 + ], + [ + -79.118261, + 36.500474 + ], + [ + -78.752366, + 36.90451 + ], + [ + -78.486458, + 37.199025 + ], + [ + -78.484901, + 37.200739 + ], + [ + -77.901571, + 37.837867 + ], + [ + -77.876673, + 37.863394 + ], + [ + -77.393333, + 38.329434 + ], + [ + -77.316978, + 38.392278 + ], + [ + -77.142041, + 38.515613 + ], + [ + -76.944906, + 38.66331 + ], + [ + -76.600846, + 39.026522 + ], + [ + -76.503308, + 39.131695 + ], + [ + -76.49731, + 39.138063 + ], + [ + -76.36397, + 39.277402 + ], + [ + -76.357148, + 39.284407 + ], + [ + -76.253686, + 39.388862 + ], + [ + -76.199156, + 39.451277 + ], + [ + -76.19441, + 39.456639 + ], + [ + -76.11108, + 39.549555 + ], + [ + -76.027365, + 39.626807 + ], + [ + -75.926979, + 39.691124 + ], + [ + -74.830576, + 40.270359 + ], + [ + -74.117474, + 40.654189 + ], + [ + -74.105964, + 40.66024 + ], + [ + -72.722634, + 41.368722 + ], + [ + -72.607247, + 41.416577 + ], + [ + -71.857247, + 41.663031 + ], + [ + -71.749709, + 41.690978 + ], + [ + -71.63763, + 41.705961 + ], + [ + -70.249995, + 41.808072 + ], + [ + -67.373069, + 43.087163 + ], + [ + -67.264327, + 43.126332 + ], + [ + -56.730997, + 46.072052 + ], + [ + -56.667531, + 46.087031 + ], + [ + -52.225183, + 46.97108 + ], + [ + -50.698953, + 47.351769 + ], + [ + -50.693671, + 47.353066 + ], + [ + -50.243671, + 47.462112 + ], + [ + -50.118883, + 47.484057 + ], + [ + -42.985553, + 48.281688 + ], + [ + -42.939641, + 48.285781 + ], + [ + -40.16339, + 48.472884 + ], + [ + -38.282053, + 48.92791 + ], + [ + -38.234557, + 48.938136 + ], + [ + -30.201227, + 50.444202 + ], + [ + -30.148882, + 50.452528 + ], + [ + -27.998882, + 50.739784 + ], + [ + -27.95207, + 50.745008 + ], + [ + -20.10207, + 51.448162 + ], + [ + -20.020101, + 51.452502 + ], + [ + -19.353431, + 51.464058 + ], + [ + -19.330236, + 51.464228 + ], + [ + -14.998453, + 51.452676 + ], + [ + -8, + 51.452676 + ], + [ + -7.964794, + 51.452141 + ], + [ + -5.848124, + 51.387777 + ], + [ + -5.828205, + 51.386998 + ], + [ + -3.928205, + 51.296222 + ], + [ + -3.912196, + 51.295344 + ], + [ + -2.578866, + 51.212823 + ], + [ + -2.564899, + 51.211872 + ], + [ + -2.431569, + 51.201969 + ], + [ + -2.311436, + 51.186477 + ], + [ + -1.911436, + 51.112178 + ], + [ + -1.905734, + 51.111102 + ], + [ + -1.690321, + 51.069824 + ], + [ + -1.296084, + 51.213329 + ], + [ + -1.142502, + 51.255085 + ], + [ + -0.959172, + 51.289757 + ], + [ + -0.861021, + 51.301202 + ], + [ + -0.851155, + 51.321531 + ], + [ + -0.784357, + 51.399565 + ], + [ + -0.694461, + 51.467853 + ], + [ + -0.584923, + 51.523819 + ], + [ + -0.459952, + 51.565362 + ], + [ + -0.324351, + 51.590925 + ], + [ + -0.18333, + 51.599553 + ], + [ + -0.042309, + 51.590925 + ], + [ + 0.093292, + 51.565362 + ], + [ + 0.218263, + 51.523819 + ], + [ + 0.327801, + 51.467853 + ], + [ + 0.417697, + 51.399565 + ], + [ + 0.484495, + 51.321531 + ], + [ + 0.52563, + 51.236708 + ], + [ + 0.539519, + 51.14833 + ], + [ + 0.539519, + 51.11472 + ], + [ + 0.587372, + 51.056421 + ], + [ + 0.625906, + 50.97467 + ], + [ + 0.639503, + 50.889679 + ], + [ + 0.627687, + 50.804427 + ], + [ + 0.590872, + 50.721918 + ], + [ + 0.530353, + 50.645076 + ], + [ + 0.448257, + 50.576637 + ], + [ + 0.347472, + 50.51905 + ], + [ + 0.23154, + 50.474382 + ], + [ + 0.104537, + 50.444243 + ], + [ + -0.02907, + 50.429721 + ], + [ + -0.6167, + 50.401534 + ], + [ + -0.643479, + 50.396374 + ], + [ + -1.237246, + 50.176258 + ], + [ + -1.372435, + 50.137267 + ], + [ + -1.517198, + 50.116754 + ], + [ + -1.665443, + 50.115606 + ], + [ + -1.810936, + 50.133874 + ], + [ + -2.324758, + 50.234252 + ], + [ + -2.66289, + 50.298254 + ], + [ + -2.728125, + 50.303194 + ], + [ + -4.046467, + 50.38639 + ], + [ + -5.928504, + 50.478081 + ], + [ + -8.017613, + 50.542864 + ], + [ + -15, + 50.542864 + ], + [ + -15.003094, + 50.542868 + ], + [ + -19.324824, + 50.554621 + ], + [ + -19.938716, + 50.54377 + ], + [ + -27.724388, + 49.832765 + ], + [ + -29.824702, + 49.54678 + ], + [ + -37.808086, + 48.022345 + ], + [ + -39.751277, + 47.543854 + ], + [ + -39.927029, + 47.516545 + ], + [ + -42.770644, + 47.321313 + ], + [ + -49.817269, + 46.518843 + ], + [ + -50.203684, + 46.423554 + ], + [ + -51.751047, + 46.030858 + ], + [ + -51.799139, + 46.019918 + ], + [ + -56.233575, + 45.122002 + ], + [ + -66.679044, + 42.152888 + ], + [ + -69.643601, + 40.814747 + ], + [ + -69.789071, + 40.764061 + ], + [ + -69.94571, + 40.738854 + ], + [ + -71.381331, + 40.631467 + ], + [ + -71.965479, + 40.436557 + ], + [ + -73.288242, + 39.749706 + ], + [ + -73.999196, + 39.361917 + ], + [ + -74.006361, + 39.358048 + ], + [ + -74.997501, + 38.827682 + ], + [ + -75.003197, + 38.82127 + ], + [ + -75.067514, + 38.746985 + ], + [ + -75.092852, + 38.719515 + ], + [ + -75.206065, + 38.60412 + ], + [ + -75.333002, + 38.470214 + ], + [ + -75.430032, + 38.364615 + ], + [ + -75.434995, + 38.359276 + ], + [ + -75.818325, + 37.950713 + ], + [ + -75.899806, + 37.877582 + ], + [ + -76.149806, + 37.688194 + ], + [ + -76.166352, + 37.676029 + ], + [ + -76.308427, + 37.574722 + ], + [ + -76.743762, + 37.150789 + ], + [ + -77.314319, + 36.521953 + ], + [ + -77.580212, + 36.224843 + ], + [ + -77.581739, + 36.223141 + ], + [ + -77.946081, + 35.817276 + ], + [ + -78.14356, + 35.591262 + ], + [ + -78.261517, + 35.349357 + ], + [ + -78.271683, + 35.329535 + ], + [ + -78.821683, + 34.303214 + ], + [ + -78.822595, + 34.30151 + ], + [ + -78.989265, + 33.990615 + ], + [ + -79.034242, + 33.919724 + ], + [ + -80.567572, + 31.825535 + ], + [ + -80.594234, + 31.791304 + ], + [ + -81.836388, + 30.294726 + ], + [ + -81.877724, + 29.40494 + ], + [ + -81.911041, + 28.665052 + ], + [ + -81.928761, + 28.548203 + ], + [ + -81.970791, + 28.436126 + ], + [ + -82.035681, + 28.332709 + ], + [ + -82.121192, + 28.241552 + ], + [ + -82.704532, + 27.72438 + ], + [ + -82.707646, + 27.72163 + ], + [ + -82.907646, + 27.545896 + ], + [ + -83.015111, + 27.468003 + ], + [ + -83.13738, + 27.409726 + ], + [ + -84.21045, + 27.009956 + ], + [ + -84.502951, + 26.281322 + ], + [ + -84.508461, + 26.26801 + ], + [ + -84.521711, + 26.237042 + ], + [ + -84.546398, + 25.975195 + ], + [ + -84.546558, + 25.973515 + ], + [ + -84.613218, + 25.284858 + ], + [ + -84.643317, + 25.14685 + ], + [ + -84.705342, + 25.017717 + ], + [ + -85.467763, + 23.799773 + ], + [ + -85.643112, + 23.071021 + ], + [ + -85.742085, + 22.630146 + ], + [ + -85.825361, + 22.256694 + ], + [ + -85.826669, + 22.250946 + ], + [ + -86.143339, + 20.882938 + ], + [ + -86.186595, + 20.756809 + ], + [ + -86.255345, + 20.6409 + ], + [ + -86.346948, + 20.539691 + ], + [ + -86.457882, + 20.457107 + ], + [ + -86.583886, + 20.396354 + ], + [ + -86.720115, + 20.359792 + ], + [ + -86.861337, + 20.348845 + ], + [ + -87.002122, + 20.363939 + ], + [ + -87.137062, + 20.404486 + ], + [ + -87.26097, + 20.468909 + ], + [ + -87.369085, + 20.554704 + ], + [ + -87.457251, + 20.658541 + ], + [ + -87.522082, + 20.776396 + ], + [ + -87.561084, + 20.903713 + ], + [ + -87.57276, + 21.035582 + ], + [ + -87.556661, + 21.166927 + ], + [ + -87.240659, + 22.529386 + ] + ] + ] + } + }, + { + "type": "Feature", + "properties": { + "stroke": "#00F", + "fill": "#00F", + "marker-color": "#00F", + "fill-opacity": 0.3 + }, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -86.85, + 21.025 + ], + [ + -86.53333, + 22.39167 + ], + [ + -86.45, + 22.765 + ], + [ + -86.35, + 23.21 + ], + [ + -86.15, + 24.04 + ], + [ + -85.33333, + 25.34167 + ], + [ + -85.26667, + 26.03 + ], + [ + -85.23333, + 26.38333 + ], + [ + -85.18333, + 26.5 + ], + [ + -84.78333, + 27.49333 + ], + [ + -83.41667, + 28 + ], + [ + -83.21667, + 28.175 + ], + [ + -82.63333, + 28.69 + ], + [ + -82.6, + 29.43 + ], + [ + -82.55, + 30.505 + ], + [ + -81.18333, + 32.14667 + ], + [ + -79.65, + 34.23333 + ], + [ + -79.48333, + 34.54333 + ], + [ + -78.93333, + 35.56667 + ], + [ + -78.78333, + 35.87333 + ], + [ + -78.53333, + 36.15833 + ], + [ + -78.16667, + 36.565 + ], + [ + -77.9, + 36.86167 + ], + [ + -77.31667, + 37.50167 + ], + [ + -76.83333, + 37.97 + ], + [ + -76.65, + 38.1 + ], + [ + -76.4, + 38.28833 + ], + [ + -76.01667, + 38.695 + ], + [ + -75.91667, + 38.80333 + ], + [ + -75.78333, + 38.94333 + ], + [ + -75.66667, + 39.06167 + ], + [ + -75.6, + 39.13833 + ], + [ + -75.51667, + 39.23167 + ], + [ + -74.41667, + 39.81667 + ], + [ + -73.7, + 40.205 + ], + [ + -72.31667, + 40.91833 + ], + [ + -71.56667, + 41.16667 + ], + [ + -70.01667, + 41.28167 + ], + [ + -67, + 42.63333 + ], + [ + -56.46667, + 45.60333 + ], + [ + -52, + 46.5 + ], + [ + -50.45, + 46.89 + ], + [ + -50, + 47 + ], + [ + -42.86667, + 47.805 + ], + [ + -40, + 48 + ], + [ + -38.03333, + 48.48 + ], + [ + -30, + 50 + ], + [ + -27.85, + 50.29 + ], + [ + -20, + 51 + ], + [ + -19.33333, + 51.01167 + ], + [ + -15, + 51 + ], + [ + -14, + 51 + ], + [ + -8.5, + 51 + ], + [ + -8.25, + 51 + ], + [ + -8, + 51 + ], + [ + -5.88333, + 50.935 + ], + [ + -3.98333, + 50.84333 + ], + [ + -2.65, + 50.76 + ], + [ + -2.51667, + 50.75 + ], + [ + -2.11667, + 50.675 + ], + [ + -1.6, + 50.575 + ], + [ + -0.93333, + 50.82 + ], + [ + -0.75, + 50.855 + ], + [ + -0.08333, + 50.88667 + ], + [ + -0.18333, + 50.985 + ], + [ + -0.18333, + 51.14833 + ] + ] + } + } + ] +} \ No newline at end of file