forked from wavded/js-shapefile-to-geojson
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
157 lines (137 loc) · 5.19 KB
/
index.html
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
<!doctype html>
<html>
<head>
<title>js-shapefile-to-geojson Demo Page</title>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css" integrity="sha512-Rksm5RenBEKSKFjgI3a41vrjkw4EVPlJ3+OiI65vTjIdo9brlAacEuKOiQ5OFh7cOI1bkDwLqdLw3Zg0cRJAAQ=="
crossorigin="" />
<style>
body {
margin: 0;
font-family: 'Roboto', sans-serif;
}
#map {
height: 800px;
background-color: #eee;
}
.upload-input {
margin: 1rem;
background-color: #03A9F4;
color: #eee;
padding: 0.5rem;
border-radius: 0.2rem;
border-width: 0;
}
#loading {
background-color: #E5EFF1;
position: absolute;
width: 100%;
height: 100%;
z-index: 999999;
opacity: 0.6;
}
.loading-img {
width: 6rem;
height: 6rem;
margin: 0 auto;
display: block;
}
.error-message {
position: absolute;
width: 100%;
height: 1.5rem;
background-color: #F44336;
color: #FFFFFF;
top: 0;
text-align: center;
padding: 1rem;
font-size: 1.4rem;
}
</style>
</head>
<body>
<div id="loading" style="display: none;">
<img class="loading-img" src="https://media1.giphy.com/media/xTk9ZvMnbIiIew7IpW/giphy.gif" alt="">
</div>
<div id="map"></div>
<script type="text/javascript" src="https://maps.google.com/maps/api/js?key=AIzaSyAQrAin98m7Nna6UEN7qpMDKp1X3Uxh0pA"></script>
<script src="/node_modules/@inlog/inlog-maps/lib/inlogMaps.js"></script>
<script src="/node_modules/leaflet.path.drag/src/Path.Drag.js"></script>
<script src="/node_modules/leaflet-editable/src/Leaflet.Editable.js"></script>
<script src="stream.js"></script>
<script src="shapefile.js"></script>
<script src="dbf.js"></script>
<div class="error-message" style="display: none;"></div>
<input id="image-file" class="upload-input" type="file" accept=".shp,.dbf" multiple enctype="multipart/form-data" />
<script type="text/javascript">
const map = new inlogMaps.Map('Google');
document.getElementById('image-file').addEventListener('change', importFiles, false);
function importFiles(data) {
showLoading();
var files = Array.from(data.target.files);
if (files.map(x => x.name.substr(x.name.lastIndexOf('.'), x.name.length)).find(x => !(x === '.shp' || x === '.dbf'))) {
showMessage('São permitidos apenas os arquivos no formato .shp ou .dbf');
return;
}
var filesGroupedByName = groupBy(files, (shapeFile) => shapeFile.name.substr(0, shapeFile.name.lastIndexOf('.')));
var fileShapes = [];
if (filesGroupedByName.size) {
filesGroupedByName.forEach(x => {
var name = x.find(x => x.name).name;
name = name.substr(0, name.lastIndexOf('.'));
var shpFile = x.find(x => x.name === `${name}.shp`);
var dbfFile = x.find(x => x.name === `${name}.dbf`);
if(!shpFile && dbfFile){
showMessage('Para cada arquivo .dbf deve existir um .shp com o mesmo nome');
return;
}
fileShapes.push(new Shapefile({
shp: shpFile,
dbf: dbfFile
}, afterParseFileShape));
});
} else {
showMessage('É necessário ao menos selecionar 1 arquivo shp');
}
}
function groupBy(list, keyGetter) {
const map = new Map();
list.forEach((item) => {
const key = keyGetter(item);
const collection = map.get(key);
if (!collection) {
map.set(key, [item]);
} else {
collection.push(item);
}
});
return map;
}
function afterParseFileShape(data) {
if (data.geojson) {
let options = {
draggable: false,
editable: false
};
map.loadGEOJson(data.geojson, options, () => { });
}
hideLoading();
}
function showMessage(text) {
var errorMessage = document.getElementsByClassName('error-message')[0]
errorMessage.innerHTML = text;
errorMessage.style.display = 'block';
hideLoading();
setTimeout(x => {
errorMessage.style.display = 'none';
}, 4000);
}
function hideLoading() {
document.getElementById('loading').style.display = 'none';
}
function showLoading() {
document.getElementById('loading').style.display = 'block';
}
</script>
</body>
</html>