forked from JeffPaine/geojson-topojson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
executable file
·128 lines (109 loc) · 5.35 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
<!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>
<br><span>* Your TopoJSON geo object must be under <code><objectName>.objects.collection</code></span>
</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 geojson = topojson.feature(parsedTopojson, parsedTopojson.objects.collection);
$('#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);
};
</script>
</body>
</html>