1
- import _ from 'underscore' ;
2
- import getDepth from 'get-depth' ;
3
- import get from '../get' ;
4
- import load from '../load' ;
5
- import utils from '../utils' ;
6
-
7
- const {
8
- categorizeIntersection,
9
- clusterLineSegments,
10
- couple,
11
- forceWithin,
12
- mergeRanges,
13
- getLineFromPoints,
14
- getIntersectionOfTwoLines,
15
- } = utils ;
1
+ /**
2
+ * @prettier
3
+ */
4
+ import _ from "underscore" ;
5
+ import getDepth from "get-depth" ;
6
+ import get from "../get" ;
7
+ import wrap from "../wrap-func" ;
8
+ import utils from "../utils" ;
9
+
10
+ const { categorizeIntersection, clusterLineSegments, couple, forceWithin, mergeRanges, getLineFromPoints, getIntersectionOfTwoLines } = utils ;
16
11
17
12
const getEdgesForPolygon = polygon => {
18
13
const edges = [ ] ;
@@ -46,7 +41,6 @@ const intersectPolygonCore = ({ georaster, geom, perPixelFunction, latlngBbox, i
46
41
if ( numRows === 0 ) return ;
47
42
48
43
for ( let y = 0 ; y < numRows ; y ++ ) {
49
-
50
44
const lat = lat0 - cellHeight * y - cellHeight / 2 ;
51
45
52
46
// use that point, plus another point along the same latitude to
@@ -60,22 +54,20 @@ const intersectPolygonCore = ({ georaster, geom, perPixelFunction, latlngBbox, i
60
54
// collapse geometry down to a list of edges
61
55
// necessary for multi-part geometries
62
56
const depth = getDepth ( geom ) ;
63
- const polygonEdges = depth === 4 ? geom . map ( getEdgesForPolygon ) : [ getEdgesForPolygon ( geom ) ] ;
57
+ const polygonEdges = depth === 4 ? geom . map ( getEdgesForPolygon ) : [ getEdgesForPolygon ( geom ) ] ;
64
58
65
59
polygonEdges . forEach ( ( edges , edgesIndex ) => {
66
60
// iterate through the list of polygon vertices, convert them to
67
61
// lines, and compute the intersections with each image row
68
62
const intersectionsByRow = _ . range ( numRows ) . map ( row => [ ] ) ;
69
63
const numberOfEdges = edges . length ;
70
64
for ( let i = 0 ; i < numberOfEdges ; i ++ ) {
71
-
72
-
73
65
// get vertices that make up an edge and convert that to a line
74
66
const edge = edges [ i ] ;
75
67
76
68
const [ startPoint , endPoint ] = edge ;
77
- const [ x1 , y1 ] = startPoint ;
78
- const [ x2 , y2 ] = endPoint ;
69
+ const [ x1 , y1 ] = startPoint ;
70
+ const [ x2 , y2 ] = endPoint ;
79
71
80
72
const direction = Math . sign ( y2 - y1 ) ;
81
73
const horizontal = y1 === y2 ;
@@ -90,18 +82,18 @@ const intersectPolygonCore = ({ georaster, geom, perPixelFunction, latlngBbox, i
90
82
91
83
let startLng , startLat , endLat , endLng ;
92
84
if ( x1 < x2 ) {
93
- [ startLng , startLat ] = startPoint ;
94
- [ endLng , endLat ] = endPoint ;
85
+ [ startLng , startLat ] = startPoint ;
86
+ [ endLng , endLat ] = endPoint ;
95
87
} else {
96
- [ startLng , startLat ] = endPoint ;
97
- [ endLng , endLat ] = startPoint ;
88
+ [ startLng , startLat ] = endPoint ;
89
+ [ endLng , endLat ] = startPoint ;
98
90
}
99
91
100
- if ( startLng === undefined ) throw Error ( ' startLng is ' + startLng ) ;
92
+ if ( startLng === undefined ) throw Error ( " startLng is " + startLng ) ;
101
93
102
94
// find the y values in the image coordinate space
103
- const imageY1 = Math . round ( ( lat0 - .5 * cellHeight - startLat ) / cellHeight ) ;
104
- const imageY2 = Math . round ( ( lat0 - .5 * cellHeight - endLat ) / cellHeight ) ;
95
+ const imageY1 = Math . round ( ( lat0 - 0.5 * cellHeight - startLat ) / cellHeight ) ;
96
+ const imageY2 = Math . round ( ( lat0 - 0.5 * cellHeight - endLat ) / cellHeight ) ;
105
97
106
98
// make sure to set the start and end points so that we are
107
99
// incrementing upwards through rows
@@ -122,11 +114,10 @@ const intersectPolygonCore = ({ georaster, geom, perPixelFunction, latlngBbox, i
122
114
for ( let j = rowStart ; j < rowEnd + 1 ; j ++ ) {
123
115
const imageLine = imageLines [ j ] ;
124
116
125
-
126
117
if ( imageLine === undefined ) {
127
- console . error ( 'j:' , j ) ;
128
- console . error ( ' imageLines:' , imageLines ) ;
129
- throw Error ( ' imageLines' ) ;
118
+ console . error ( "j:" , j ) ;
119
+ console . error ( " imageLines:" , imageLines ) ;
120
+ throw Error ( " imageLines" ) ;
130
121
}
131
122
132
123
// because you know x is zero in ax + by = c, so by = c and b = -1, so -1 * y = c or y = -1 * c
@@ -183,7 +174,7 @@ const intersectPolygonCore = ({ georaster, geom, perPixelFunction, latlngBbox, i
183
174
vertical,
184
175
xmin : xminOnLine ,
185
176
xmax : xmaxOnLine ,
186
- imageLineY,
177
+ imageLineY
187
178
} ) ;
188
179
}
189
180
}
@@ -193,15 +184,15 @@ const intersectPolygonCore = ({ georaster, geom, perPixelFunction, latlngBbox, i
193
184
if ( segmentsInRow . length > 0 ) {
194
185
const clusters = clusterLineSegments ( segmentsInRow , numberOfEdges ) ;
195
186
const categorized = clusters . map ( categorizeIntersection ) ;
196
- const [ throughs , nonthroughs ] = _ . partition ( categorized , item => item . through ) ;
187
+ const [ throughs , nonthroughs ] = _ . partition ( categorized , item => item . through ) ;
197
188
198
189
if ( throughs . length % 2 === 1 ) {
199
- throw Error ( ' throughs.length for ' + rowIndex + ' is odd with ' + throughs . length ) ;
190
+ throw Error ( " throughs.length for " + rowIndex + " is odd with " + throughs . length ) ;
200
191
}
201
192
202
193
let insides = nonthroughs . map ( intersection => [ intersection . xmin , intersection . xmax ] ) ;
203
194
204
- const sortedThroughs = _ . sortBy ( throughs , ' xmin' ) ;
195
+ const sortedThroughs = _ . sortBy ( throughs , " xmin" ) ;
205
196
206
197
const couples = couple ( sortedThroughs ) . map ( couple => {
207
198
const [ left , right ] = couple ;
@@ -217,12 +208,11 @@ const intersectPolygonCore = ({ georaster, geom, perPixelFunction, latlngBbox, i
217
208
insides = mergeRanges ( insides ) ;
218
209
219
210
insides . forEach ( pair => {
220
-
221
211
const [ xmin , xmax ] = pair ;
222
212
223
213
//convert left and right to image pixels
224
- const left = Math . round ( ( xmin - ( lng0 + .5 * cellWidth ) ) / cellWidth ) ;
225
- const right = Math . round ( ( xmax - ( lng0 + .5 * cellWidth ) ) / cellWidth ) ;
214
+ const left = Math . round ( ( xmin - ( lng0 + 0.5 * cellWidth ) ) / cellWidth ) ;
215
+ const right = Math . round ( ( xmax - ( lng0 + 0.5 * cellWidth ) ) / cellWidth ) ;
226
216
227
217
const startColumnIndex = Math . max ( left , 0 ) ;
228
218
const endColumnIndex = Math . min ( right , imageWidth ) ;
@@ -245,20 +235,9 @@ const intersectPolygon = (georaster, geom, perPixelFunction) => {
245
235
// get values in a bounding box around the geometry
246
236
const latlngBbox = utils . getBoundingBox ( geom ) ;
247
237
248
- if ( typeof georaster === "string" ) {
249
- return load ( georaster ) . then ( loaded => {
250
- return get ( georaster , latlngBbox ) . then ( imageBands => (
251
- intersectPolygonCore ( { georaster : loaded , geom, perPixelFunction, latlngBbox, imageBands } )
252
- ) ) ;
253
- } ) ;
254
- } else if ( ! georaster . values ) {
255
- return get ( georaster , latlngBbox ) . then ( imageBands => (
256
- intersectPolygonCore ( { georaster, geom, perPixelFunction, latlngBbox, imageBands } )
257
- ) ) ;
258
- } else {
259
- const imageBands = get ( georaster , latlngBbox ) ;
260
- return intersectPolygonCore ( { georaster, geom, perPixelFunction, latlngBbox, imageBands } ) ;
261
- }
238
+ const values = get ( georaster , latlngBbox ) ;
239
+
240
+ return utils . callAfterResolveArgs ( imageBands => intersectPolygonCore ( { georaster, geom, perPixelFunction, latlngBbox, imageBands } ) , values ) ;
262
241
} ;
263
242
264
- export default intersectPolygon ;
243
+ export default wrap ( intersectPolygon ) ;
0 commit comments