This repository has been archived by the owner on Oct 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.html
executable file
·224 lines (198 loc) · 9.32 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
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
<!DOCTYPE html>
<html class="no-js">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>GeoJSON-TopoJSON</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/normalize/2.1.0/normalize.css">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap.min.css">
<style>
body {
padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
}
</style>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap-responsive.min.css">
</head>
<body>
<a href="https://github.com/JeffPaine/geojson-topojson"><img style="position: absolute; top: 0; right: 0; border: 0; z-index:1050;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png" alt="Fork me on GitHub"></a>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="#">GeoJSON-TopoJSON</a>
</div>
</div>
</div>
<div class="container">
<div class="row" id="error">
</div>
<div class="row">
<div class="span6">
<h2>GeoJSON</h2>
<textarea class="span6" rows="20" id="geojson"></textarea>
<button class="btn btn-large btn-primary" id="convert-to-topojson" type="button">Convert to TopoJSON <i class="icon-arrow-right icon-white"></i></button>
</div>
<div class="span6">
<h2>TopoJSON</h2>
<textarea class="span6" rows="20" id="topojson"></textarea>
<button class="btn btn-large btn-primary" id="convert-to-geojson" type="button"><i class="icon-arrow-left icon-white"></i> Convert to GeoJSON</button>
</div>
</div>
<hr>
<div class="row">
<p>For an interactive map tool to convert GeoJSON -> TopoJSON, check out <a href="http://shancarter.github.io/distillery/">distillery</a>.</p>
</div>
<div class="row">
<footer>
<hr>
<p class="pull-right text-right">Crafted with love and ⌘-R by <a href="https://twitter.com/japaine">Jeff Paine</a>.</p>
</footer>
</div>
</div> <!-- END container -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.1/js/bootstrap-alert.js"></script>
<script src="lib.js"></script>
<script>
// Click handler for our convert to TopoJSON button
$('#convert-to-topojson').click(function() {
var geojsonString = $('#geojson').val();
// Clear the contents of our TopoJSON textarea
$('#topojson').val('');
if (geojsonString === '') {
// Ignore empty submissions
} else {
convertGeojsonToTopojson(geojsonString);
}
});
// Click handler for our convert to GeoJSON button
$('#convert-to-geojson').click(function() {
var topojsonString = $('#topojson').val();
// Clear the contents of our GeoJSON textarea
$('#geojson').val('');
if (topojsonString === '') {
// Ignore empty submissions
} else {
convertTopojsonToGeojson(topojsonString);
}
});
// Convert the given GeoJSON to TopoJSON
var convertGeojsonToTopojson = function(geojsonString) {
var parsedGeojson = JSON.parse(geojsonString);
var topojsonOutput = topojson.topology({
collection: parsedGeojson
});
$('#topojson').val(JSON.stringify(topojsonOutput, null, 4));
};
// Convert the given TopoJSON to GeoJSON
var convertTopojsonToGeojson = function(topojsonString) {
try {
var parsedTopojson = JSON.parse(topojsonString);
var geJSONobj = new GeoJSON();
//iterate over each key in the objects of the topojson
for (var col in parsedTopojson.objects) {
if (parsedTopojson.objects.hasOwnProperty(col)) {
var gJ = topojson.feature(parsedTopojson, parsedTopojson.objects[col]);
//merge with the existing GeoJSON Object
geJSONobj.merge(gJ);
}
}
//get the complete GeoJSON data
var geojson = geJSONobj.getData();
//Write it to the geojson text box
$('#geojson').val(JSON.stringify(geojson, null, 4));
} catch (error) {
displayError('There was an unknown error converting your TopoJSON to GeoJSON. Sorry.');
console.log(error, error.message);
}
};
// Make our error alert box closable
$(".alert").alert();
// Display errors to the user
var displayError = function(errorText) {
var html = '<div class="span12 alert alert-error"><button type="button" class="close" data-dismiss="alert">×</button><b>Error: </b> ';
html += errorText;
html += '</div>';
$('#error').html(html);
};
//function for building GeoJSON:
function GeoJSON() {
var data;
this.merge = function(input) {
if (this.data == null) {
this.data = input;
return;
}
//Data already exists, we need to look at the type
var type = this.data.type;
switch (type) {
case "FeatureCollection":
//Featurecollection already exists. We just need to add the Features from the input
// to the data's Features
this.data.features = this.data.features.concat(this.getFeatures(input));
break;
case "Feature":
// we need to create a new FeatureCollection & then concatenate the input
var ob = {
"type": "FeatureCollection",
"features": [this.data]
};
//now set the data to this new FeatureCollection
this.data = ob;
this.data.features = this.data.features.concat(this.getFeatures(input));
break;
//For the 7 types of Geometry objects, We need to make the FeatureCollection & then concatenate
case "Point":
case "MultiPoint":
case "LineString":
case "MultiLineString":
case "Polygon":
case "MultiPolygon":
case "GeometryCollection":
var ob = {
"type": "FeatureCollection",
"features": this.getFeatures(this.data)
};
this.data = ob;
this.data.features = this.data.features.concat(this.getFeatures(input));
break;
default:
//UnExpected data type
throw "UnExpected data type";
}
};
this.getFeatures = function(geoJSON) {
var type = geoJSON.type;
switch (type) {
case "FeatureCollection":
return geoJSON.features;
case "Feature":
return [geoJSON];
//For the 7 types of Geometry objects, just fall through to makeFeaturesArray object
case "Point":
case "MultiPoint":
case "LineString":
case "MultiLineString":
case "Polygon":
case "MultiPolygon":
case "GeometryCollection":
return this.makeFeaturesArray(geoJSON);
default:
//UnExpected Input; Return Empty Array
return [];
}
};
this.makeFeaturesArray = function(geom) {
var feature = {
"type": "Feature",
"geometry": geom //Note: There can't be properties.
};
return [feature];
};
this.getData = function() {
return this.data;
};
}
</script>
</body>
</html>