1
0
mirror of https://github.com/wavded/js-shapefile-to-geojson synced 2024-11-23 14:34:54 +00:00

Merge branch 'master' into gh-pages

This commit is contained in:
Marc Harter 2013-09-09 18:39:44 -05:00
commit 885ee16fcd
2 changed files with 45 additions and 29 deletions

View File

@ -7,42 +7,52 @@ Inspired by the excellent work by Tom Carden ([http://github.com/RandomEtc/shape
I just got this out there so nothing is minified. See index.html for an example of order. All files need to be in the same directory. This will use Web Workers if the browser support exists. Not recommended for large files, more of an experiment than anything.
### Running the Example
Host the files (example won't run off disk, needs to be ran on a web server), a simple way to do this is run the following command in the project root:
```sh
python -m SimpleHTTPServer
```
And visit http://localhost:8000 in your favorite browser.
### Usage
You can use it to parse shapefiles (.shp) or dBase files (.dbf) or both. Here are some examples.
Load Shapefile Only
var shapefile = new Shapefile("myshapefile.shp",function(data){
new Shapefile("myshapefile.shp", function (data) {
// data returned
};
});
Load DBF Only
var dbf = new DBF("mydbf.dbf",function(data){
new DBF("mydbf.dbf", function (data) {
// data returned
};
});
Load Shapefile w/ DBF Attributes
var shapefile = new Shapefile({
shp: "myshape.dbf",
new Shapefile({
shp: "myshape.shp",
dbf: "myshape.dbf"
}, function(data){
}, function (data) {
// data returned
};
});
Use with OpenLayers
var
parser = new OpenLayer.Format.GeoJSON(),
features,
shapefile = new Shapefile({
shp: "myshape.dbf",
dbf: "myshape.dbf"
}, function(data){
features = parser.read(data.geojson);
};
var parser = new OpenLayer.Format.GeoJSON(),
features;
new Shapefile({
shp: "myshape.shp",
dbf: "myshape.dbf"
}, function (data) {
features = parser.read(data.geojson);
});
### Resources

View File

@ -1,14 +1,18 @@
(function(window,undefined){
if(window.document && window.Worker){
var worker = new Worker("shapefile.js")
var worker = null;
var Shapefile = function(o, callback){
var
w = this.worker = worker,
t = this,
o = typeof o == "string" ? {shp: o} : o
if (!worker) {
var path = (o.jsRoot || "") + "shapefile.js"
var w = worker = this.worker = new Worker(path)
}
w.onmessage = function(e){
t.data = e.date
if(callback) callback(e.data)
@ -69,7 +73,7 @@
xhr.send()
if(200 != xhr.status)
throw "Unable to load " + o.shp + " status: " + xhr.status
throw "Unable to load " + o.shp + " status: " + xhr.status
this.url = o.shp
this.stream = new Gordon.Stream(xhr.responseText)
@ -85,7 +89,7 @@
that.addDBFDataToGeoJSON(data)
that._postMessage()
})
else this._postMessage
else this._postMessage()
}
Shapefile.prototype = {
@ -256,20 +260,22 @@
for (var r = 0, record; record = records[r]; r++){
feature = {}, fbounds = record.bounds, points = record.points, parts = record.parts
feature.type = "Feature"
feature.bbox = [
fbounds.left,
fbounds.bottom,
fbounds.right,
fbounds.top
]
if (record.shapeType !== 'Point') {
feature.bbox = [
fbounds.left,
fbounds.bottom,
fbounds.right,
fbounds.top
]
}
geometry = feature.geometry = {}
switch (record.shapeType) {
case "Point":
geometry.type = "Point"
geometry.coordinates = [
record.points.x,
record.points,y ]
record.x,
record.y ]
break
case "MultiPoint":
case "PolyLine":