Giter VIP home page Giter VIP logo

js-shapefile-to-geojson's Introduction

This project allows a user to load Shapefiles and DBFs into the browser with JavaScript. Outputs as GeoJSON for use with other Mapping APIs such as OpenLayers.

Inspired by the excellent work by Tom Carden (http://github.com/RandomEtc/shapefile-js/).

Overview

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:

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

new Shapefile("myshapefile.shp", function (data) {
    // data returned
});

Load DBF Only

new DBF("mydbf.dbf", function (data) {
    // data returned
});

Load Shapefile w/ DBF Attributes

new Shapefile({
    shp: "myshape.shp",
    dbf: "myshape.dbf"
}, function (data) {
    // data returned
});

Use with OpenLayers

var parser = new OpenLayer.Format.GeoJSON(),
    features;

new Shapefile({
    shp: "myshape.shp",
    dbf: "myshape.dbf"
}, function (data) {
    features = parser.read(data.geojson);
});

Resources

I used the technical descriptions found here to parse the binary:

ESRI Shapefile Technical Description - PDF

dBase (Xbase) File Format Description

Future

I plan to implement (time permitting) some custom renderers like SVG or Canvas (besides using OpenLayers) to improve the speed.

Feel free to hack at this, submit bugs, pull requests, and make it better. If you write a renderer, please push it back and I'll add it to the project.

License

(The MIT License)

Copyright (c) 2010 Marc Harter <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

js-shapefile-to-geojson's People

Contributors

kmanzana avatar underbluewaters avatar wavded avatar zhiweitian avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

js-shapefile-to-geojson's Issues

Add a License

Hi,

I'd love to make use of your code in one of my projects, but I was wondering if you might first add a license to it.

feature request

It would be cool to add the ability to load the projection info from a .prj file (and convert it into something openlayers/leaflet can use like EPSG:4326" ).
In my experience most shapefiles come in some obscure projection, so this could be very usefull.

typo?

Hi, this is good stuff.

For the nitpickers among us: In the file shapefile.js on line 19 it shows: t.data = e.date. I was wondering if this is a typo and should read e.data...? I changed it and it didn't seem to hurt.

Other question: I appreciate this works asynchronously. For small files it would be nice to do it synchronously. You would be able to return shp = new Shapefile(shpname,callback) with the shp.data object set. The code is quick enough to allow for that. Anyway, my two cents...

Firefox read blob error

Hi,

This library works well in Chrome and Internet Explorer, but it fails in Firefox. It never makes it to the function here:
var shapefile = new Shapefile(
{
shp : me.shpurl,
dbf : me.dbfurl
}, function(data)
{
debugger; // never gets here
var features = parser.read(data.geojson);
vector.addFeatures(features);
});

Any ideas? Here is a screenshot of Firebug's output.

error

js-shapefile-to-geojson read chinese(solved)

¤j®a¦n(大家好)
I can not find how to convert to chinese
I do not very know unicode
but I try big-5 , utf-8 and convert online tool
but it is still wrong

please help me
thank !

Reading Shapefile using file input crashing google chrome

I have this code:

var opts = { shp: files[0], jsRoot: "/Scripts/ShapeReader/" };
var shapefile = new Shapefile(opts, function (data) {
    console.log(data);
});

Here files is the value of a file input.
Now, this code works fine in Firefox (27.0.1) but Google Chrome (33.0.1750.154 m) crashes when this code executes.

To be specific, the error occurs when executing line 23 of shapefile.js:
w.postMessage(["Load", o])

no hardcoded paths please

My files or not in the root of the web-directory but in src/shapefile.
In order to get it working I had to change the hardcoded paths in two locations:

  • shapefile.js, line 12: var path = (o.jsRoot || "") + "shapefile.js"
  • dbf.js, line 4: var worker = new Worker("dbf.js")

Thanks for this great tool!
Alex

Subsequent request fails

    var pnt =  {
        radius: 3,
        fillColor: "#009688",
        color: "#000",
        weight: 1,
        opacity: 1,
        fillOpacity: 0.8
    };
    var area =  {
        weight: 2,
        color: "#999",
        opacity: 1,
        fillColor: "#B0DE5C",
        fillOpacity: 0.5
    };
    parser = L.geoJson([], {
        style: function (feature) {
            return feature.properties && area;
        }
    }).addTo(map);
    var starttime = +new Date;
    shapefile = new Shapefile({
        shp: "/data/1.shp",
        dbf: "/data/1.dbf"
    }, function(data){
        features = parser.addData(data.geojson);
        console.log("took",new Date - starttime,"milliseconds")
    })
    parser1 = L.geoJson([], {
        pointToLayer: function (feature, latlng) {
            return L.circleMarker(latlng, pnt);
        }
    }).addTo(map);
    var starttime1 = +new Date;
    shapefile1 = new Shapefile({
        shp: "/data/2.shp",
        dbf: "/data/2.dbf"
    }, function(data){
        features1 = parser1.addData(data.geojson);
        console.log("took",new Date - starttime1,"milliseconds")
    })

when using the above code, only shapefile1 is visible and there is no error.

Can not load shapefile

i am from indonesia.
I have 2 shape file, but both can not be loaded by shapefile.js. one of them appeared error

  1. fbounds is undefined
  2. window.shapefile is undefined -> window.shapefile.addDBFDataToGeoJSON (e.data [1])
    what kind of bugs are in shapefile.js??
    sdfgsgtery

and lastly, one shape file is not executable by shapefile.js
  varshape var = new Shapefile ({
         shp: link + ". shp",
         dbf: link + ". dbf"
     }, Function (data) { ...... }

Problem with polyline shape file

Sample Shape.txt

Hello,
I use your code to convert shape files to json and to show on the map. In most cases it works wonderful, thank you very much. But I have some shapefile that is shown wrong. I attach sample images:
sample1.jpg shows how this file is shown in http://www.mapshaper.org/ and it is what I expect.
sample1_wrong.jpg shows what I get using your code. Also I wanted to attach sample.shp,
but this site does not allow me to do that. I can send it to you if you'll explain me how to do that.
Updated: I added Sample.shp as SampleShape.txt.
Any help will be appreciated
sample1_wrong
sample1

ask - multiple shape file

I want to developt gis using this sample. But how about using multiple shape file?? What should i do??

ShapeFile Object is undefined

I downloaded the project and ran the index.html file, but I got an error on chrome and firefox. Not sure if you were interested in knowing.

Error:
Uncaught ReferenceError: ShapeFile is not defined

Using with React Native

Is possible to use this on a React Native app? I need to import shp file and plot on a map. Thanks.

How to create drill down

I want to create drilldown on shapes that is if i am clicking on any shape it should show the nested child shape files of that particular shape.for example if i am clicking on provinces of any country it should load the details of that provinces

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.