1.1.0 • Published 3 months ago

shp-to-geojson v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

shp-to-geojson

  • A small library for converting shapefiles to geojson
  • Usable in both node and the browser (~30kb in the browser minified with all dependencies).
  • Works with remote url's, filepaths (inc zipped) in node, or arraybuffer's (eg for in drag-drop scenarios in the browser)
  • Offers a streaming API as well, as a method to return the whole FeatureCollection.

API

import ShpToGeoJson from 'shp-to-geojson'
// Or in the browser
import ShpToGeoJson from 'shp-to-geojson/dist/shp-to-geojson.browser.js'

// Loading From a FilePath in NodeJS
const shp = new ShpToGeoJson({
  filePath: "./test/points.shp"
})

// Get a GeoJson FeatureCollection
const featureCollection = shp.getGeoJson()

// Get a stream of features
const stream = shp.streamGeoJsonFeatures()
let featureIterator = stream.next()
while (!featureIterator.done) {
    const feature = featureIterator.value
    featureIterator = stream.next()
}

More Usage Examples

Options

OptionDescriptionExample
remotePathA url to a .shphttps://someurl/points.shp
filePathA filepath resolved in NodeJS using fs.loadFiledata/countries.shp
filePathZippedA zipped file available in NodeJSdata/countries.zip
arraybuffersAn object containing shpBuffer, dbfBuffer, and projString keys{shpBuffer: new ArrayBuffer(), dbfBuffer: new ArrayBuffer(), projString: ''}

Methods

MethodReturnsDescription
loadPromiseRequired if loading using the remotePath option
getGeoJsonGeojson FeatureCollectionReturns a geojson FeatureCollection
streamGeoJsonFeaturesGeneratorReturns a js generator for iterating through features

Properties

PropertyDescription
summaryReturns the type of feature, number of features, bbox, and crs. Requires .load() to be have been called when using the remotePath option
loadedReturns true/false whether the data has been loaded.

Performance

This library appears to perform very well when there are lots of attributes compared to most other js libraries out there.

3,000 polygons with 40 attributes
shp-to-geojson x 13.68 ops/sec ±3.37% (39 runs sampled)
shpjs x 3.19 ops/sec ±2.75% (12 runs sampled)
shp x 10.60 ops/sec ±6.74% (31 runs sampled)
shapefile x 1.10 ops/sec ±3.14% (10 runs sampled)
- Fastest is shp-to-geojson

Note that the shp library while fast, it only works in NodeJS, and doesn't parse attributes nicely.

When there are minimal attributes this lib performs pretty similarly to other libs out there.

200 countries with a single attribute
shp-to-geojson x 67.47 ops/sec ±3.61% (63 runs sampled)
shpjs x 63.74 ops/sec ±2.83% (64 runs sampled)
shapefile x 57.47 ops/sec ±7.71% (69 runs sampled)
- Fastest is shp-to-geojson

Motivations

  1. I found the API's of some of the existing libraries pretty unintuitive
  2. Some of them were only compatible with node (eg shp), I wanted something that worked in both browser and node with the same API
  3. Some struggled with parsing attributes nicely (eg shp)
  4. I wanted something that worked with z values.
  5. I was intrigued to have a go at writing my first parsing library and getting experience with raw buffer data.

Background Docs

Thanks

Thanks to creators/maintainers of other shapefiles parsers out there, I leaned on some of their buffer reading skills to grow my own understanding.

1.1.0

3 months ago

1.0.0

8 months ago

0.0.1

12 months ago