-
Notifications
You must be signed in to change notification settings - Fork 47
/
shp.html
131 lines (110 loc) · 3.95 KB
/
shp.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
<html>
<head>
<title>Javascript Shapefile 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>
<!--[if IE]><script src="lib/excanvas.js"></script><![endif]-->
<script type="text/javascript">
var file = "thematicmapping/TM_WORLD_BORDERS_SIMPL-0.3.shp";
window.onload = function() {
var b = new BinaryAjax(file, onBinaryAjaxComplete, onBinaryAjaxFail);
}
function onBinaryAjaxFail() {
alert('failed to load ' + file);
}
function onBinaryAjaxComplete(oHTTP) {
var binFile = oHTTP.binaryResponse;
if (window.console && window.console.log) console.log('got data, parsing shapefile');
var 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: "+shp.shapeType+")");
}
//if (window.console && window.console.log) console.log(records);
render(shpFile.records);
}
function render(records) {
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 = '#888888';
ctx.fillStyle = '#fff8f0';
ctx.beginPath();
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.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');
}
</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 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="http://github.com/RandomEtc/shapefile-js">http://github.com/RandomEtc/shapefile-js</a> for more details.</p>
</body>
</html>