@@ -133,7 +133,7 @@ export namespace Measurement {
133
133
* 经纬度格式化
134
134
* @param position 经纬度 lng=position[0] lat=position[1]
135
135
*/
136
- format ? ( position : GeoJSON . Position ) : string
136
+ format ( position : GeoJSON . Position ) : string
137
137
}
138
138
139
139
/**
@@ -147,7 +147,7 @@ export namespace Measurement {
147
147
* @param end 是否为最后一个
148
148
* @param segment 是否为中间数值
149
149
*/
150
- format ? ( length : number , index : number , end : boolean , segment : boolean ) : string ,
150
+ format ( length : number , index : number , end : boolean , segment : boolean ) : string ,
151
151
152
152
/**
153
153
* 是否包含第一个数值
@@ -158,22 +158,22 @@ export namespace Measurement {
158
158
}
159
159
160
160
type TMeasureCalPolygonOptions = {
161
- format ? ( area : number ) : string ,
161
+ format ( area : number ) : string ,
162
162
withLineString ?: boolean ,
163
- measureLineStringOptions ? : TMeasureCalLineStringOptions ,
163
+ measureLineStringOptions : TMeasureCalLineStringOptions ,
164
164
}
165
165
166
166
/**
167
167
* 测量计算参数
168
168
*/
169
- type TMeasureCalOptions = {
170
- point ? : TMeasureCalPointOptions ,
171
- lineString ? : TMeasureCalLineStringOptions ,
172
- polygon ? : TMeasureCalPolygonOptions
169
+ export type TMeasureCalOptions = {
170
+ point : TMeasureCalPointOptions ,
171
+ lineString : TMeasureCalLineStringOptions ,
172
+ polygon : TMeasureCalPolygonOptions
173
173
}
174
174
175
- function calPoint ( g : GeoJSON . Position , options ? : TMeasureCalPointOptions ) : TMeasureMarkerFeature [ ] {
176
- const value = options ? .format ?. ( g ) || g [ 0 ] . toFixed ( 6 ) + ',' + g [ 1 ] . toFixed ( 6 ) ;
175
+ function calPoint ( g : GeoJSON . Position , options : TMeasureCalPointOptions ) : TMeasureMarkerFeature [ ] {
176
+ const value = options . format ( g ) ;
177
177
return [ {
178
178
type : "Feature" ,
179
179
geometry : {
@@ -187,7 +187,7 @@ export namespace Measurement {
187
187
} ]
188
188
}
189
189
190
- function calLineString ( g : GeoJSON . Position [ ] , options ? : TMeasureCalLineStringOptions , isPolygon : boolean = false ) : TMeasureMarkerFeature [ ] {
190
+ function calLineString ( g : GeoJSON . Position [ ] , options : TMeasureCalLineStringOptions , isPolygon : boolean = false ) : TMeasureMarkerFeature [ ] {
191
191
const ret = new Array < TMeasureMarkerFeature > ( ) ;
192
192
let sumLength = 0 ;
193
193
@@ -213,7 +213,7 @@ export namespace Measurement {
213
213
coordinates : c . geometry . coordinates
214
214
} ,
215
215
properties : {
216
- value : options ? .format ?. ( l , i , false , true ) || ` ${ l . toFixed ( 2 ) } m` ,
216
+ value : options . format ( l , i , false , true ) ,
217
217
type : isPolygon ? 'polygon-line-segment' : 'line-segment'
218
218
}
219
219
} ) ;
@@ -229,7 +229,7 @@ export namespace Measurement {
229
229
coordinates : current
230
230
} ,
231
231
properties : {
232
- value : options ? .format ?. ( sumLength , i , i === g . length - 1 , false ) || ` ${ sumLength . toFixed ( 2 ) } m` ,
232
+ value : options . format ( sumLength , i , i === g . length - 1 , false ) ,
233
233
type : isPolygon ? 'polygon-line' : 'line'
234
234
}
235
235
} ) ;
@@ -238,12 +238,12 @@ export namespace Measurement {
238
238
return ret ;
239
239
}
240
240
241
- function calPolygon ( g : GeoJSON . Position [ ] [ ] , options ? : TMeasureCalPolygonOptions ) : TMeasureMarkerFeature [ ] {
241
+ function calPolygon ( g : GeoJSON . Position [ ] [ ] , options : TMeasureCalPolygonOptions ) : TMeasureMarkerFeature [ ] {
242
242
const ret = new Array < TMeasureMarkerFeature > ( ) ;
243
243
244
244
if ( options ?. withLineString !== false ) {
245
245
g . forEach ( x => {
246
- ret . push ( ...calLineString ( x , options ? .measureLineStringOptions ) ) ;
246
+ ret . push ( ...calLineString ( x , options . measureLineStringOptions , true ) ) ;
247
247
} ) ;
248
248
}
249
249
@@ -259,7 +259,7 @@ export namespace Measurement {
259
259
coordinates : center . geometry . coordinates
260
260
} ,
261
261
properties : {
262
- value : options ? .format ?. ( a ) || ` ${ a . toFixed ( 2 ) } m²` ,
262
+ value : options . format ( a ) ,
263
263
type : 'polygon'
264
264
}
265
265
} )
@@ -268,20 +268,20 @@ export namespace Measurement {
268
268
return ret ;
269
269
}
270
270
271
- function calGeometry ( g : GeoJSON . Geometry , options ? : TMeasureCalOptions ) : TMeasureMarkerFeature [ ] {
272
- if ( g . type === 'Point' ) return calPoint ( g . coordinates , options ? .point ) ;
273
- if ( g . type === 'LineString' ) return calLineString ( g . coordinates , options ? .lineString ) ;
274
- if ( g . type === 'Polygon' ) return calPolygon ( g . coordinates , options ? .polygon ) ;
271
+ function calGeometry ( g : GeoJSON . Geometry , options : TMeasureCalOptions ) : TMeasureMarkerFeature [ ] {
272
+ if ( g . type === 'Point' ) return calPoint ( g . coordinates , options . point ) ;
273
+ if ( g . type === 'LineString' ) return calLineString ( g . coordinates , options . lineString ) ;
274
+ if ( g . type === 'Polygon' ) return calPolygon ( g . coordinates , options . polygon ) ;
275
275
276
276
function calMulGeometry < T > ( data : Array < T > , cal : ( d : T , options ?: any ) => TMeasureMarkerFeature [ ] , options : any ) : TMeasureMarkerFeature [ ] {
277
277
return data . reduce ( ( p , c ) => {
278
278
return p . concat ( cal ( c , options ) ) ;
279
279
} , new Array < TMeasureMarkerFeature > ( ) ) ;
280
280
}
281
281
282
- if ( g . type === 'MultiPoint' ) return calMulGeometry ( g . coordinates , calPoint , options ? .point ) ;
283
- if ( g . type === 'MultiLineString' ) return calMulGeometry ( g . coordinates , calLineString , options ? .lineString ) ;
284
- if ( g . type === 'MultiPolygon' ) return calMulGeometry ( g . coordinates , calPolygon , options ? .polygon ) ;
282
+ if ( g . type === 'MultiPoint' ) return calMulGeometry ( g . coordinates , calPoint , options . point ) ;
283
+ if ( g . type === 'MultiLineString' ) return calMulGeometry ( g . coordinates , calLineString , options . lineString ) ;
284
+ if ( g . type === 'MultiPolygon' ) return calMulGeometry ( g . coordinates , calPolygon , options . polygon ) ;
285
285
286
286
throw new Error ( `not suppose geometry type: ${ g . type } to measure` ) ;
287
287
}
@@ -292,7 +292,7 @@ export namespace Measurement {
292
292
* @param options 计算参数
293
293
* @returns
294
294
*/
295
- export function cal ( data : GeoJSON . Feature | GeoJSON . Feature [ ] | GeoJSON . Geometry , options ? : TMeasureCalOptions ) {
295
+ export function cal ( data : GeoJSON . Feature | GeoJSON . Feature [ ] | GeoJSON . Geometry , options : TMeasureCalOptions ) {
296
296
if ( data instanceof Array ) {
297
297
return data . reduce ( ( p , c ) => {
298
298
const features = calGeometry ( c . geometry , options ) ;
0 commit comments