UNPKG

4.29 kBMarkdownView Raw
1# Shapefile.js
2
3Pure JavaScript library for parsing Shapefiles, returns Geojson projected into WGS84 lat lons.
4
5## Usage
6
7For use in node, rollup, webpack and where ever ESM modules are used we have a lovely package you can install via npm or yarn or whatever.
8
9 npm install shpjs --save
10
11If you need a stand alone file to include in your webpage the old fashioned way then you can grab the built version that's either included in the repo or you can use unpkg.
12
13 https://unpkg.com/shpjs@latest/dist/shp.js
14
15or
16
17 https://unpkg.com/shpjs@latest/dist/shp.min.js
18
19
20When using this library in some sort of bundler for the browser, no polyfills for node apis are required, the only thing needed is some sort of dependency resolver plugin like [rollup node-resolve](https://www.npmjs.com/package/@rollup/plugin-node-resolve) if your bundler doesn't have it, you are almost certainly already using one to get this library anyway.
21
22Addtionally you can import it directly into an esm based web script with
23
24```js
25import shp from 'https://unpkg.com/shpjs@latest/dist/shp.esm.js'
26```
27
28## API
29
30There are 3 ways to use it:
31
321\. you can pass it a url to a shapefile, either to the shapefile itself (with or without the .shp suffix)
33
34```javascript
35 import shp from 'shpjs';
36 //for the shapefiles in the folder called 'files' with the name pandr.shp
37 const geojson = await shp("files/pandr.shp");
38```
39
40or you can pass it a url to a a .zip file which contains the shapefile
41
42```javascript
43 //for the shapefiles in the files folder called pandr.shp
44 const geojson = await shp("files/pandr.zip");
45```
46
472\. you can pass in in a binary buffer (ArrayBuffer, TypedArray, DataView, Node Buffer) containing a zip file containing at least one shapefile like from a file in node:
48
49```javascript
50// in node
51const data = await fs.readFile('./path/to/shp.zip');
52const geojson = await shp(data);
53```
54or the [File API](https://developer.mozilla.org/en-US/docs/Web/API/File) in the browser
55
56```javascript
57// in browser from some sort of file upload
58const data = await file.arrayBuffer()
59const geojson = await shp(data);
60```
61
623\. You can pass in an object with `shp`, `dbf`, `prj`, and `cpg` properties.
63
64```javascript
65const object = {}
66object.shp = await fs.readFile('./path/to/file.shp');
67// dbf is optional, but needed if you want attributes
68object.dbf = await fs.readFile('./path/to/file.dbf');
69// prj is optional, but needed if your file is in some projection you don't want it in
70object.prj = await fs.readFile('./path/to/file.prj');
71// cpg is optional but needed if your dbf is in some weird (non utf8) encoding.
72object.cpg = await fs.readFile('./path/to/file.cpg');
73
74const geojson = await shp(object)
75```
76
77## on zipfiles
78
79If there is only one shp in the zipefile it returns geojson, if there are multiple then it will be an array. All of the geojson objects have an extra key `fileName` the value of which is the
80name of the shapefile minus the extension (I.E. the part of the name that's the same for all of them).
81
82
83# links
84
85- [wikipedia article](https://en.wikipedia.org/wiki/Shapefile)
86- [ESRI white paper](http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf)
87- [This page on Xbase](http://www.clicketyclick.dk/databases/xbase/format/dbf.html)
88
89## Demos
90
91- [Countries/zipfile](http://calvinmetcalf.github.io/shapefile-js)
92- [Google maps](http://calvinmetcalf.github.io/shapefile-js/site/map.html)
93- [Local Zipfile](http://leaflet.calvinmetcalf.com)
94- [Projected big with web workers](http://calvinmetcalf.github.io/shapefile-js/site/proj.html)
95
96## About
97
98Descended in a ship of theseus way from [RandomEtc's shapefile library](https://github.com/RandomEtc/shapefile-js) no code is shared.
99
100- [World Borders shapefile](http://thematicmapping.org/downloads/world_borders.php) is CC-BY-SA 3.0.
101- Park and Ride shapefile is from [MassDOT](http://mass.gov/massdot) and is public domain.
102- MA town boundaries from [MassGIS](http://www.mass.gov/anf/research-and-tech/it-serv-and-support/application-serv/office-of-geographic-information-massgis/) and is public domain
103- NJ County Boundaries from [NJgin](https://njgin.state.nj.us/NJ_NJGINExplorer/index.jsp) and should be public domain.
104- [Proj4js](https://github.com/proj4js/proj4js) by me et al MIT
105- Other test datasets copyright their respective owners.
\No newline at end of file