From 0b14898be1b678d7866edefe0e49faabad702a58 Mon Sep 17 00:00:00 2001 From: Marc Harter Date: Thu, 30 May 2013 09:14:59 -0500 Subject: [PATCH 1/6] fix; examples --- readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index dfb7d7e..ee2dafd 100644 --- a/readme.md +++ b/readme.md @@ -26,7 +26,7 @@ Load DBF Only Load Shapefile w/ DBF Attributes var shapefile = new Shapefile({ - shp: "myshape.dbf", + shp: "myshape.shp", dbf: "myshape.dbf" }, function(data){ // data returned @@ -38,7 +38,7 @@ Use with OpenLayers parser = new OpenLayer.Format.GeoJSON(), features, shapefile = new Shapefile({ - shp: "myshape.dbf", + shp: "myshape.shp", dbf: "myshape.dbf" }, function(data){ features = parser.read(data.geojson); From 907d16da837406bd508572c9350514024dc87078 Mon Sep 17 00:00:00 2001 From: Marc Harter Date: Sun, 25 Aug 2013 22:20:33 -0500 Subject: [PATCH 2/6] updated; usage instructions --- readme.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/readme.md b/readme.md index ee2dafd..1ba7823 100644 --- a/readme.md +++ b/readme.md @@ -7,6 +7,16 @@ 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. From db9bfd55d87e4ef77a8f8e03bd8b2ec3cc8c2bf5 Mon Sep 17 00:00:00 2001 From: Marc Harter Date: Mon, 26 Aug 2013 16:58:35 -0500 Subject: [PATCH 3/6] updated; code samples --- readme.md | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/readme.md b/readme.md index 1ba7823..8921663 100644 --- a/readme.md +++ b/readme.md @@ -23,36 +23,36 @@ You can use it to parse shapefiles (.shp) or dBase files (.dbf) or both. Here a 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({ + 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.shp", - 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 From 30d3272537dd2c69d75bdcfb940305245ae8e93d Mon Sep 17 00:00:00 2001 From: Chad Burt Date: Mon, 9 Sep 2013 15:30:01 -0700 Subject: [PATCH 4/6] Ensuring callback provided to Shapefile constructor is executed even if a dbf file is not provided --- shapefile.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shapefile.js b/shapefile.js index f050d4e..3630e87 100644 --- a/shapefile.js +++ b/shapefile.js @@ -85,7 +85,7 @@ that.addDBFDataToGeoJSON(data) that._postMessage() }) - else this._postMessage + else this._postMessage() } Shapefile.prototype = { From 825ce8af25cc63a522e79e8a8e2439daa10d16f2 Mon Sep 17 00:00:00 2001 From: Chad Burt Date: Mon, 9 Sep 2013 15:45:38 -0700 Subject: [PATCH 5/6] Fixed support for Point datasets While some support existed, an attempt was being made to create an unnecessary(?) bounding box for each point feature. Also swapped access of the x,y coordinates from record.geometry.x|y to record.x|y. --- shapefile.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/shapefile.js b/shapefile.js index f050d4e..6fe83bb 100644 --- a/shapefile.js +++ b/shapefile.js @@ -256,20 +256,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": From c4cf216ec4683ce28b6e8d324ab1065635731e85 Mon Sep 17 00:00:00 2001 From: Chad Burt Date: Mon, 9 Sep 2013 16:07:10 -0700 Subject: [PATCH 6/6] Add jsRoot constructor option Workers were being constructed with a hardcoded path of 'shapefile.js'. The new jsRoot option can be used to specify an alternate location. For example: shapefile = new Shapefile({ shp: "testdata/world.shp", dbf: "testdata/world.dbf", jsRoot: '/js-shapefile-to-geojson/' }, function(data) ... --- shapefile.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/shapefile.js b/shapefile.js index f050d4e..9be3bb6 100644 --- a/shapefile.js +++ b/shapefile.js @@ -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)