forked from frankrowe/react-native-geojson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
118 lines (106 loc) · 3.61 KB
/
index.js
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
import React, { Component, PropTypes } from 'react';
import { View } from 'react-native';
import MapView from 'react-native-maps';
import uuid from 'uuid';
export const makeOverlays = features => {
const points = features
.filter(f => f.geometry && (f.geometry.type === 'Point' || f.geometry.type === 'MultiPoint'))
.map(feature => makeCoordinates(feature).map(coordinates => makeOverlay(coordinates, feature)))
.reduce(flatten, [])
.map(overlay => ({ ...overlay, type: 'point' }));
const lines = features
.filter(
f => f.geometry && (f.geometry.type === 'LineString' || f.geometry.type === 'MultiLineString')
)
.map(feature => makeCoordinates(feature).map(coordinates => makeOverlay(coordinates, feature)))
.reduce(flatten, [])
.map(overlay => ({ ...overlay, type: 'polyline' }));
const multipolygons = features
.filter(f => f.geometry && f.geometry.type === 'MultiPolygon')
.map(feature => makeCoordinates(feature).map(coordinates => makeOverlay(coordinates, feature)))
.reduce(flatten, []);
const polygons = features
.filter(f => f.geometry && f.geometry.type === 'Polygon')
.map(feature => makeOverlay(makeCoordinates(feature), feature))
.reduce(flatten, [])
.concat(multipolygons)
.map(overlay => ({ ...overlay, type: 'polygon' }));
return points.concat(lines).concat(polygons);
};
const flatten = (prev, curr) => prev.concat(curr);
const makeOverlay = (coordinates, feature) => {
let overlay = {
feature,
id: feature.id ? feature.id : uuid(),
};
if (feature.geometry.type === 'Polygon' || feature.geometry.type === 'MultiPolygon') {
overlay.coordinates = coordinates[0];
if (coordinates.length > 1) {
overlay.holes = coordinates.slice(1);
}
} else {
overlay.coordinates = coordinates;
}
return overlay;
};
const makePoint = c => ({ latitude: c[1], longitude: c[0] });
const makeLine = l => l.map(makePoint);
const makeCoordinates = feature => {
const g = feature.geometry;
if (g.type === 'Point') {
return [makePoint(g.coordinates)];
} else if (g.type === 'MultiPoint') {
return g.coordinates.map(makePoint);
} else if (g.type === 'LineString') {
return [makeLine(g.coordinates)];
} else if (g.type === 'MultiLineString') {
return g.coordinates.map(makeLine);
} else if (g.type === 'Polygon') {
return g.coordinates.map(makeLine);
} else if (g.type === 'MultiPolygon') {
return g.coordinates.map(p => p.map(makeLine));
} else {
return [];
}
};
const Geojson = props => {
const overlays = makeOverlays(props.geojson.features);
return (
<React.Fragment key={props.geojson.id || uuid()}>
{overlays.map(overlay => {
if (overlay.type === 'point') {
return (
<MapView.Marker
key={overlay.id}
coordinate={overlay.coordinates}
pinColor={props.color}
/>
);
}
if (overlay.type === 'polygon') {
return (
<MapView.Polygon
key={overlay.id}
coordinates={overlay.coordinates}
holes={overlay.holes}
strokeColor={props.strokeColor}
fillColor={props.fillColor}
strokeWidth={props.strokeWidth}
/>
);
}
if (overlay.type === 'polyline') {
return (
<MapView.Polyline
key={overlay.id}
coordinates={overlay.coordinates}
strokeColor={props.strokeColor}
strokeWidth={props.strokeWidth}
/>
);
}
})}
</React.Fragment>
);
};
export default Geojson;