Shapefile SHP

Read and write Shapefile SHP and SHX files within Node JS.
IMPORTANT: This is an ESM NodeJS only package.

Installation

Contents

Introduction

A Shapefile is a selection of files grouped together into a ZIP file. Each file contains different information that together is used to detail geographic map information. It comes with a *.shp file that gives the vector data, a *.shx file that is an index of the vector data, and a *.dbf file that is a dBase 3 database file containing attribute data about the different map areas.

This package is used to read and write the *.shp and *.shx data. Each type of shape has its own class object, which allows you to create a Shapefile SHP object and have it written to a file (with the index).

The simplist method of loading in all the shapefile SHP data is to use the load function.

Here we are using the index file to quickly find the location of the third polygon and read it in.

Below are the different shape type objects that can be used. You will need to search for the shapefile file format to better understand how the information is used to create map information.

Quick Functions

There are some static functions that can help you perform some quick tasks.

Load

Load all the vector data from the shapefile SHP file into a ShapefileShp object.

Arguments

  • shpPath - The full path of the shapefile SHP file to open.

Returns

A promise that resolves with a ShapefileShp object.

Example


Save

Save the ShapefileShp object to a shapefile SHP file. This also creates the *.shx index file.

Arguments

  • shpPath - The full path of the shapefile SHP file to open.
  • shapefileShp - The ShapefileShp object that contains all the shape information.

Returns

A promise.

Example

Shapefile SHP Reader

This class is used to read in shapefile SHP data.

Open

Open the shapefile SHP file.

Arguments

  • shpPath - The full path of the shapefile SHP file to open.

Returns

A promise.

Example


Close

Close the already opened shapefile SHP file.

Returns

A promise.

Example


Read

Read in the next shape within the shapefile SHP file.

Arguments

  • [offset] - The offset within the shapefile SHP file where the shape record begins. This offset normally comes from the index file. If not used then the next shape record will be read.

Returns

A promise resolving to a one of the available shape objects, or null if the end of the file is reached.

Example


Properties

There are a number of different properties available.

After opening the file you can get the shapefile SHP header information.

After reading in a record you can get it's record number value, which starts at 1.

After reading in a record you can get the total content length (in bytes) that was read in.

After reading in the record you can get the offset of the record where it was located within the SHP file.

Shapefile SHP Writer

This class is used to create shapefile SHP data.

Open

Open the shapefile SHP file to write to. This will remove any existing file.

Arguments

  • shpPath - The full path of the shapefile SHP file to open.

Returns

A promise.

Example


Close

Close the shapefile SHP file.

Returns

A promise.

Example


Write

Write a shape object to the shapefile SHP file.

Arguments

  • shape - The shape object to write. This can be one of the may different shape objects available.

Returns

A promise.

Example


Update Header

Update the ShapefileSHP object header information in the shapefile SHP file. As you write more and more shapes to the file, the overall file length will increase. Also, the minimum and maximum ranges will also be updated. After you have finished writing all the shapes to the file, you can update the properties within the ShapefileShp object and have it written to the file.

Arguments

  • shapefileShp - The shapefile SHP object that contains the header information that will get written to the file.

Returns

A promise.

Example


Properties

There are a number of different properties available.

After writing a record you can get it's record number value, which starts at 1.

After writing a record you can get the total content length (in bytes) that was just written in.

After writing the record you can get the offset of the record where it is located within the SHP file.

Shapefile SHX Reader

This class is used to read in shapefile SHX index files.

Open

Open the shapefile SHX file.

Arguments

  • shxPath - The full path of the shapefile SHX file to open.

Returns

A promise.

Example


Close

Close the shapefile SHX file.

Returns

A promise.

Example


Read

Read in the next shape index within the shapefile SHX file.

Arguments

  • [number] - The record number to get the index for. If this is not used then the next index is read in. You can use this to get the index of a shape record anywhere within the SHX file.

Returns

A promise resolving to a ShapeIndex object or null if the end of the file is reached.

Example


Properties

There are a number of different properties available.

After the file has been open you can get the ShapefileShx header object.

Shapefile SHX Writer

This class is used to create shapefile SHX index files.

Open

Open the shapefile SHX index file to write to. This will remove any existing file.

Arguments

  • shxPath - The full path of the shapefile SHX index file to open.

Returns

A promise.

Example


Close

Close the shapefile SHX file.

Returns

A promise.

Example


Write

Write new index information to the shapefile SHX file.

Arguments

  • offset - The offset position location within the SHP file where the shape record begins.
  • contentLength - The total content size of the shape record (not including the record header).

Returns

A promise.

Example


Update Header

Update the ShapefileShx object header information in the shapefile SHX file. As you write more and more indexes to the file, the overall file length will increase. You may also need to update the minimum and maximum ranges. After you have finished writing all the indexes, you will need to update the ShapefileShx object and have it written to the SHX file.

Arguments

  • shapefileShx - The shapefile SHX object that contains the header information that will get written to the file.

Returns

A promise.

Example