npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

shp-to-geojson

v1.1.0

Published

- 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

Downloads

1,948

Readme

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

Option | Description | Example ------------ | ------------- | ------------- remotePath | A url to a .shp | https://someurl/points.shp filePath | A filepath resolved in NodeJS using fs.loadFile | data/countries.shp filePathZipped | A zipped file available in NodeJS | data/countries.zip arraybuffers | An object containing shpBuffer, dbfBuffer, and projString keys | {shpBuffer: new ArrayBuffer(), dbfBuffer: new ArrayBuffer(), projString: ''}

Methods

Method | Returns | Description ------------ | ------------- | ------------- load | Promise | Required if loading using the remotePath option getGeoJson | Geojson FeatureCollection | Returns a geojson FeatureCollection streamGeoJsonFeatures | Generator | Returns a js generator for iterating through features

Properties

Property | Description -------------- | ------------- summary | Returns the type of feature, number of features, bbox, and crs. Requires .load() to be have been called when using the remotePath option loaded | Returns 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.