1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
import React, { CSSProperties, SVGProps, useMemo, useEffect, useState } from 'react'
import { PigeonProps, Point } from '../types'
interface GeoJsonProps extends PigeonProps {
className?: string
data?: any
svgAttributes?: any
styleCallback?: any
hover?: any
feature?: any
style?: CSSProperties
children?: React.ReactNode
// callbacks
onClick?: ({ event: HTMLMouseEvent, anchor: Point, payload: any }) => void
onContextMenu?: ({ event: HTMLMouseEvent, anchor: Point, payload: any }) => void
onMouseOver?: ({ event: HTMLMouseEvent, anchor: Point, payload: any }) => void
onMouseOut?: ({ event: HTMLMouseEvent, anchor: Point, payload: any }) => void
}
interface GeoJsonLoaderProps extends GeoJsonProps {
link?: string
}
interface GeoJsonGeometry {
type: string
coordinates?:
| [number, number]
| Array<[number, number]>
| Array<Array<[number, number]>>
| Array<Array<Array<[number, number]>>>
geometries?: Array<GeoJsonGeometry>
}
interface GeometryProps {
coordinates?:
| [number, number]
| Array<[number, number]>
| Array<Array<[number, number]>>
| Array<Array<Array<[number, number]>>>
latLngToPixel?: (latLng: Point, center?: Point, zoom?: number) => Point
svgAttributes?: SVGProps<SVGElement>
geometry?: GeoJsonGeometry
}
const defaultSvgAttributes = { fill: '#93c0d099', strokeWidth: '2', stroke: 'white', r: '30' }
export function PointComponent(props: GeometryProps): JSX.Element {
const { latLngToPixel } = props
const [y, x] = props.coordinates as [number, number]
const [cx, cy] = latLngToPixel([x, y])
if (props.svgAttributes?.path) {
const path = `M${cx},${cy}c${props.svgAttributes.path.split(/[c|C|L|l|v|V|h|H](.*)/s)[1]}`
return <path d={path} {...(props.svgAttributes as SVGProps<SVGCircleElement>)} />
}
return <circle cx={cx} cy={cy} {...(props.svgAttributes as SVGProps<SVGCircleElement>)} />
}
export function MultiPoint(props: GeometryProps): JSX.Element {
return (
<>
{props.coordinates.map((point, i) => (
<PointComponent {...props} coordinates={point} key={i} />
))}
</>
)
}
export function LineString(props: GeometryProps): JSX.Element {
const { latLngToPixel } = props
const p =
'M' +
(props.coordinates as Array<[number, number]>).reduce((a, [y, x]) => {
const [v, w] = latLngToPixel([x, y])
return a + ' ' + v + ' ' + w
}, '')
return <path d={p} {...(props.svgAttributes as SVGProps<SVGPathElement>)} />
}
export function MultiLineString(props: GeometryProps): JSX.Element {
return (
<>
{props.coordinates.map((line, i) => (
<LineString {...props} coordinates={line} key={i} />
))}
</>
)
}
export function Polygon(props: GeometryProps): JSX.Element {
const { latLngToPixel } = props
// GeoJson polygons is a collection of linear rings
const p = (props.coordinates as Array<Array<[number, number]>>).reduce(
(a, part) =>
a +
' M' +
part.reduce((a, [y, x]) => {
const [v, w] = latLngToPixel([x, y])
return a + ' ' + v + ' ' + w
}, '') +
'Z',
''
)
return <path d={p} {...(props.svgAttributes as SVGProps<SVGPathElement>)} />
}
export function MultiPolygon(props: GeometryProps): JSX.Element {
return (
<>
{props.coordinates.map((polygon, i) => (
<Polygon {...props} coordinates={polygon} key={i} />
))}
</>
)
}
export function GeometryCollection(props: GeometryProps): JSX.Element {
const renderer = {
Point: PointComponent,
MultiPoint,
LineString,
MultiLineString,
Polygon,
MultiPolygon,
}
const { type, coordinates, geometries } = props.geometry
if (type === 'GeometryCollection') {
return (
<>
{geometries.map((geometry, i) => (
<GeometryCollection key={i} {...props} geometry={geometry} />
))}
</>
)
}
const Component = renderer[type]
if (Component === undefined) {
console.warn(`The GeoJson Type ${type} is not known`)
return null
}
return (
<Component
latLngToPixel={props.latLngToPixel}
geometry={props.geometry}
coordinates={coordinates}
svgAttributes={props.svgAttributes}
/>
)
}
export function GeoJsonFeature(props: GeoJsonProps): JSX.Element {
const [internalHover, setInternalHover] = useState(props.hover || false)
const hover = props.hover !== undefined ? props.hover : internalHover
const callbackSvgAttributes = props.styleCallback && props.styleCallback(props.feature, hover)
const svgAttributes = callbackSvgAttributes
? props.svgAttributes
? { ...props.svgAttributes, ...callbackSvgAttributes }
: callbackSvgAttributes
: props.svgAttributes
? props.svgAttributes
: defaultSvgAttributes
const eventParameters = (event: React.MouseEvent<SVGElement>) => ({
event,
anchor: props.anchor,
payload: props.feature,
})
return (
<g
clipRule="evenodd"
style={{ pointerEvents: 'auto' }}
onClick={props.onClick ? (event) => props.onClick(eventParameters(event)) : null}
onContextMenu={props.onContextMenu ? (event) => props.onContextMenu(eventParameters(event)) : null}
onMouseOver={(event) => {
props.onMouseOver && props.onMouseOver(eventParameters(event))
setInternalHover(true)
}}
onMouseOut={(event) => {
props.onMouseOut && props.onMouseOut(eventParameters(event))
setInternalHover(false)
}}
>
<GeometryCollection {...props} {...props.feature} svgAttributes={svgAttributes} />
</g>
)
}
export function GeoJson(props: GeoJsonProps): JSX.Element {
const { width, height } = props.mapState
return (
<div
style={{
position: 'absolute',
left: '0',
top: '0',
pointerEvents: 'none',
cursor: 'pointer',
...(props.style || {}),
}}
className={props.className ? `${props.className} pigeon-click-block` : 'pigeon-click-block'}
>
<svg
width={width}
height={height}
viewBox={`0 0 ${width} ${height}`}
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
{props.data && props.data.features.map((feature, i) => <GeoJsonFeature key={i} {...props} feature={feature} />)}
{React.Children.map(props.children, (child) => {
if (!child) {
return null
}
if (!React.isValidElement(child)) {
return child
}
return React.cloneElement(child, props)
})}
</svg>
</div>
)
}
export function GeoJsonLoader(props: GeoJsonLoaderProps): JSX.Element {
const [data, setData] = useState(props.data ? props.data : null)
useEffect(() => {
fetch(props.link)
.then((response) => response.json())
.then((data) => setData(data))
}, [props.link])
return data ? <GeoJson data={data} {...props} /> : null
}
|