-
Notifications
You must be signed in to change notification settings - Fork 47
/
index.html
170 lines (141 loc) · 5.12 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
<html>
<head>
<title>Javascript Shapefile and DBF Loader</title>
<script type="text/javascript" src="lib/binaryajax.js"></script>
<script type="text/javascript" src="src/binarywrapper.js"></script>
<script type="text/javascript" src="src/shapefile.js"></script>
<script type="text/javascript" src="src/dbf.js"></script>
<!--[if IE]><script src="lib/excanvas.js"></script><![endif]-->
<script type="text/javascript">
var shpURL = "thematicmapping/TM_WORLD_BORDERS_SIMPL-0.3.shp";
var dbfURL = "thematicmapping/TM_WORLD_BORDERS_SIMPL-0.3.dbf";
var shpFile;
var dbfFile;
window.onload = function() {
var shpLoader = new BinaryAjax(shpURL, onShpComplete, onShpFail);
var dbfLoader = new BinaryAjax(dbfURL, onDbfComplete, onDbfFail);
}
function onShpFail() {
alert('failed to load ' + shpURL);
}
function onDbfFail() {
alert('failed to load ' + dbfURL);
}
function onShpComplete(oHTTP) {
var binFile = oHTTP.binaryResponse;
if (window.console && window.console.log) console.log('got data, parsing shapefile');
shpFile = new ShpFile(binFile);
if (shpFile.header.shapeType != ShpType.SHAPE_POLYGON && shpFile.header.shapeType != ShpType.SHAPE_POLYLINE) {
alert("Shapefile does not contain Polygon records (found type: "+shpFile.header.shapeType+")");
}
//if (window.console && window.console.log) console.log(records);
if (dbfFile) {
render(shpFile.records, dbfFile.records);
}
}
function onDbfComplete(oHTTP) {
var binFile = oHTTP.binaryResponse;
if (window.console && window.console.log) console.log('got data, parsing dbf file');
dbfFile = new DbfFile(binFile);
//if (window.console && window.console.log) console.log(dbfFile.records);
if (shpFile) {
render(shpFile.records, dbfFile.records);
}
}
function render(records, data) {
if (window.console && window.console.log) console.log('creating canvas and rendering');
var canvas = document.getElementById('map');
if (window.G_vmlCanvasManager) {
G_vmlCanvasManager.initElement(canvas);
}
var t1 = new Date().getTime();
if (window.console && window.console.log) console.log('calculating bbox...');
var box;
for (var i = 0; i < records.length; i++) {
var record = records[i];
if (record.shapeType == ShpType.SHAPE_POLYGON || record.shapeType == ShpType.SHAPE_POLYLINE) {
var shp = record.shape
for (var j = 0; j < shp.rings.length; j++) {
var ring = shp.rings[j];
for (var k = 0; k < ring.length; k++) {
if (!box) {
box = { x: ring[k].x, y: ring[k].y, width: 0, height: 0 };
}
else {
var l = Math.min(box.x, ring[k].x);
var t = Math.min(box.y, ring[k].y);
var r = Math.max(box.x+box.width, ring[k].x);
var b = Math.max(box.y+box.height, ring[k].y);
box.x = l;
box.y = t;
box.width = r-l;
box.height = b-t;
}
}
}
}
}
var t2 = new Date().getTime();
if (window.console && window.console.log) console.log('found bbox in ' + (t2 - t1) + ' ms');
t1 = new Date().getTime();
if (window.console && window.console.log) console.log('starting rendering...');
var ctx = canvas.getContext('2d');
var sc = Math.min(800 / box.width, 400 / box.height);
ctx.fillStyle = '#ccccff';
ctx.fillRect(0,0,800,400);
ctx.lineWidth = 0.5;
ctx.strokeStyle = '#ffffff';
for (var i = 0; i < records.length; i++) {
var record = records[i];
if (record.shapeType == ShpType.SHAPE_POLYGON || record.shapeType == ShpType.SHAPE_POLYLINE) {
var shp = record.shape;
for (var j = 0; j < shp.rings.length; j++) {
var ring = shp.rings[j];
if (ring.length < 1) continue;
ctx.fillStyle = getFillRecord(data[i]);
ctx.beginPath();
ctx.moveTo((ring[0].x - box.x) * sc, 400 - (ring[0].y - box.y) * sc);
for (var k = 1; k < ring.length; k++) {
ctx.lineTo((ring[k].x - box.x) * sc, 400 - (ring[k].y - box.y) * sc);
}
ctx.fill();
ctx.stroke();
}
}
}
t2 = new Date().getTime();
if (window.console && window.console.log) console.log('done rendering in ' + (t2 - t1) + ' ms');
}
function getFillRecord(record) {
var colors = ["#F1EEF6", "#D4B9DA", "#C994C7", "#DF65B0", "#DD1C77", "#980043"]
var popn = parseInt(record.values['POP2005']);
if (!isNaN(popn)) {
popn = Math.max(0, Math.min(100000000, popn));
var colorIndex = parseInt((colors.length-1) * popn / 100000000);
var color = colors[colorIndex];
//if (window.console && window.console.log) console.log('popn: ' + popn + ' color: ' + color);
return color;
}
return '#000000';
}
</script>
<style type="text/css">
body {
background-color: #eee;
color: #000;
font: 12px sans-serif;
margin: 20px;
}
canvas {
background-color: #fff;
padding: 10px;
}
</style>
</head>
<body>
<h1>Javascript Shapefile and DBF Loader</h1>
<p>Loading shapefile... uninformative errors may appear in the dark crevices of your browser.</p>
<canvas id="map" width="800" height="400"></canvas>
<p>See <a href="https://github.com/RandomEtc/shapefile-js">https://github.com/RandomEtc/shapefile-js</a> for more details.</p>
</body>
</html>