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. 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 ### Usage
You can use it to parse shapefiles (.shp) or dBase files (.dbf) or both. Here are some examples. You can use it to parse shapefiles (.shp) or dBase files (.dbf) or both. Here are some examples.
Load Shapefile Only Load Shapefile Only
var shapefile = new Shapefile("myshapefile.shp",function(data){ new Shapefile("myshapefile.shp", function (data) {
// data returned // data returned
}; });
Load DBF Only Load DBF Only
var dbf = new DBF("mydbf.dbf",function(data){ new DBF("mydbf.dbf", function (data) {
// data returned // data returned
}; });
Load Shapefile w/ DBF Attributes Load Shapefile w/ DBF Attributes
var shapefile = new Shapefile({ new Shapefile({
shp: "myshape.dbf", shp: "myshape.shp",
dbf: "myshape.dbf" dbf: "myshape.dbf"
}, function(data){ }, function (data) {
// data returned // data returned
}; });
Use with OpenLayers Use with OpenLayers
var var parser = new OpenLayer.Format.GeoJSON(),
parser = new OpenLayer.Format.GeoJSON(), features;
features,
shapefile = new Shapefile({ new Shapefile({
shp: "myshape.dbf", shp: "myshape.shp",
dbf: "myshape.dbf" dbf: "myshape.dbf"
}, function(data){ }, function (data) {
features = parser.read(data.geojson); features = parser.read(data.geojson);
}; });
### Resources ### Resources

View File

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