@@ -4,8 +4,11 @@ import {
4
4
} from "@arcgis/core/core/accessorSupport/decorators" ;
5
5
import * as promiseUtils from "@arcgis/core/core/promiseUtils" ;
6
6
import * as watchUtils from "@arcgis/core/core/watchUtils" ;
7
+ import { Polygon } from "@arcgis/core/geometry" ;
8
+ import FeatureLayer from "@arcgis/core/layers/FeatureLayer" ;
7
9
import { tsx } from "@arcgis/core/widgets/support/widget" ;
8
10
import Widget from "@arcgis/core/widgets/Widget" ;
11
+ import simplify from "simplify-js" ;
9
12
import { countryLabel } from "./countryUtils" ;
10
13
import { map , palette , view } from "./globals" ;
11
14
import RegionEditor from "./RegionEditor" ;
@@ -35,7 +38,7 @@ export class PlaceCountry extends Widget {
35
38
) ;
36
39
}
37
40
38
- private async queryRegion ( e : MouseEvent ) {
41
+ private async queryRegion ( e : MouseEvent , serverSide = false ) {
39
42
const include = map . allLayers . filter (
40
43
( l ) => l . type === "feature" || l . type === "graphics"
41
44
) ;
@@ -56,7 +59,23 @@ export class PlaceCountry extends Widget {
56
59
return layerIndex ( b ) - layerIndex ( a ) ;
57
60
} ) ;
58
61
59
- return results [ 0 ] . graphic ;
62
+ const graphic = results [ 0 ] . graphic ;
63
+ const layer = graphic . layer ;
64
+ if (
65
+ layer . type === "feature" &&
66
+ layer instanceof FeatureLayer &&
67
+ serverSide
68
+ ) {
69
+ const query = layer . createQuery ( ) ;
70
+ query . returnGeometry = true ;
71
+ query . objectIds = [ graphic . getObjectId ( ) ] ;
72
+ query . outSpatialReference = graphic . geometry . spatialReference ;
73
+ const response = await layer . queryFeatures ( query ) ;
74
+ if ( response . features . length ) {
75
+ return response . features [ 0 ] ;
76
+ }
77
+ }
78
+ return graphic ;
60
79
}
61
80
}
62
81
@@ -110,7 +129,7 @@ export class PlaceCountry extends Widget {
110
129
return ;
111
130
}
112
131
113
- const graphic = await this . queryRegion ( e ) ;
132
+ const graphic = await this . queryRegion ( e , true ) ;
114
133
115
134
if ( graphic ) {
116
135
const color = palette . selectionColor ;
@@ -119,10 +138,47 @@ export class PlaceCountry extends Widget {
119
138
} else if ( color ) {
120
139
e . preventDefault ( ) ;
121
140
141
+ const clone = graphic . clone ( ) ;
142
+ const geometry = clone . geometry as Polygon ;
143
+
144
+ // The further the country is in the north / south, the more we have to simplify
145
+ const ymax = Math . max (
146
+ Math . abs ( geometry . extent . ymax ) ,
147
+ Math . abs ( geometry . extent . ymin )
148
+ ) ;
149
+ const tolerance = ymax / 2500 ;
150
+
151
+ let rings = geometry . rings ;
152
+ const maxLength = rings . reduce (
153
+ ( acc , cur ) => Math . max ( cur . length , acc ) ,
154
+ 0
155
+ ) ;
156
+
157
+ // Filter small islands around main land
158
+ rings = rings . filter ( ( r ) => r . length > maxLength / 10 ) ;
159
+
160
+ let before = 0 ;
161
+ let after = 0 ;
162
+
163
+ // Simplify remaining rings
164
+ rings = rings . map ( ( ring ) => {
165
+ const points = ring . map ( ( c ) => {
166
+ return { x : c [ 0 ] , y : c [ 1 ] } ;
167
+ } ) ;
168
+ before += points . length ;
169
+ const simplified = simplify ( points , tolerance ) ;
170
+ after += simplified . length ;
171
+ return simplified . map ( ( p ) => [ p . x , p . y ] ) ;
172
+ } ) ;
173
+
174
+ // console.log({ before, after, countr: countryLabel(clone) });
175
+
176
+ geometry . rings = rings ;
177
+
122
178
this . regionEditor . editSelection (
123
179
new SelectedRegion ( {
124
180
color,
125
- graphic,
181
+ graphic : clone ,
126
182
} )
127
183
) ;
128
184
this . resetRegion ( ) ;
0 commit comments