Code Monkey home page Code Monkey logo

shapefile's Introduction

Streaming Shapefile Parser

In Node:

var shapefile = require("shapefile");

shapefile.open("example.shp")
  .then(source => source.read()
    .then(function log(result) {
      if (result.done) return;
      console.log(result.value);
      return source.read().then(log);
    }))
  .catch(error => console.error(error.stack));

In a browser:

<!DOCTYPE html>
<script src="https://unpkg.com/[email protected]"></script>
<script>

shapefile.open("https://cdn.rawgit.com/mbostock/shapefile/master/test/points.shp")
  .then(source => source.read()
    .then(function log(result) {
      if (result.done) return;
      console.log(result.value);
      return source.read().then(log);
    }))
  .catch(error => console.error(error.stack));

</script>

In a terminal:

shp2json example.shp

For a live example, see bl.ocks.org/2dd741099154a4da55a7db31fd96a892. See also ndjson-cli for examples of manipulating GeoJSON using newline-delimited JSON streams.

This parser implementation is based on the ESRI Shapefile Technical Description and dBASE Table File Format. Caveat emptor: this is a work in progress and does not currently support all shapefile geometry types. It only supports dBASE III and has little error checking. Please contribute if you want to help!

In-browser parsing of dBASE table files requires TextDecoder, part of the Encoding living standard, which is not supported in IE or Safari as of September, 2016. See text-encoding for a browser polyfill.

API Reference

# shapefile.open(shp[, dbf[, options]]) <>

Returns a promise that yields an open shapefile source.

If typeof shp is “string”, opens the shapefile at the specified shp path. If shp does not have a “.shp” extension, it is implicitly added. If shp instanceof ArrayBuffer or shp instanceof Uint8Array, reads the specified in-memory shapefile. Otherwise, shp must be a Node readable stream in Node or a WhatWG standard readable stream in browsers.

If typeof dbf is “string”, opens the dBASE file at the specified dbf path. If dbf does not have a “.dbf” extension, it is implicitly added. If dbf instanceof ArrayBuffer or dbf instanceof Uint8Array, reads the specified in-memory dBASE file. If dbf is undefined and shp is a string, then dbf defaults to shp with the “.shp” extension replaced with “.dbf”; in this case, no error is thrown if there is no dBASE file at the resulting implied dbf. If dbf is undefined and shp is not a string, or if dbf is null, then no dBASE file is read, and the resulting GeoJSON features will have empty properties. Otherwise, dbf must be a Node readable stream in Node or a WhatWG standard readable stream in browsers.

If typeof shp or dbf is “string”, in Node, the files are read from the file system; in browsers, the files are read using streaming fetch, if available, and falling back to XMLHttpRequest. See path-source for more.

The follwing options are supported:

  • encoding - the dBASE character encoding; defaults to “windows-1252”
  • highWaterMark - in Node, the size of the stream’s internal buffer; defaults to 65536

# shapefile.read(shp[, dbf[, options]]) <>

Returns a promise that yields a GeoJSON feature collection for specified shapefile shp and dBASE table file dbf. The meaning of the arguments is the same as shapefile.open. This is a convenience API for reading an entire shapefile in one go; use this method if you don’t mind putting the whole shapefile in memory. The yielded collection has a bbox property representing the bounding box of all records in this shapefile. The bounding box is specified as [xmin, ymin, xmax, ymax], where x and y represent longitude and latitude in spherical coordinates.

# source.bbox

The shapefile’s bounding box [xmin, ymin, xmax, ymax], where x and y represent longitude and latitude in spherical coordinates.

# source.read() <>

Returns a Promise for the next record from the underlying stream. The yielded result is an object with the following properties:

  • value - a GeoJSON feature, or undefined if the stream ended
  • done - a boolean which is true if the stream ended

# source.cancel() <>

Returns a Promise which is resolved when the underlying stream has been destroyed.

Command Line Reference

# shp2json [options…] [file]

Converts the specified shapefile file to a GeoJSON. If file is not specified, defaults to reading from stdin (with no dBASE file). For example, to convert to a feature collection:

shp2json example.shp

To convert to a geometry collection:

shp2json -g example.shp

To convert to newline-delimited features:

shp2json -n example.shp

To convert to newline-delimited geometries:

shp2json -ng example.shp

When --geometry or --ignore-properties is not used, the shapefile is joined to the dBASE table file (.dbf) file corresonding to the specified shapefile file, if any.

# shp2json -h
# shp2json --help

Output usage information.

# shp2json -V
# shp2json --version

Output the version number.

# shp2json -o file
# shp2json --out file

Specify the output file name. Defaults to “-” for stdout.

# shp2json -n
# shp2json --newline-delimited

Output newline-delimited JSON, with one feature or geometry per line.

# shp2json -g
# shp2json --geometry

Output a geometry collection instead of a feature collection or, in conjuction with --newline-delimited, geometry objects instead of feature objects. Implies --ignore-properties.

# shp2json --ignore-properties

Ignore the corresponding dBASE table file (.dbf), if any. Output features will have an empty properties object.

# shp2json --encoding encoding

Specify the dBASE table file character encoding. Defaults to “windows-1252”.

shapefile's People

Contributors

mbostock avatar jasondavies avatar frogcat avatar mhall2 avatar

Watchers

Jeremy Sarda avatar James Cloos avatar

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.