GeoSHPer

GeoSHPer is a JavaScript library for converting shapefile data into GeoJSON. It reads ZIP archives containing .shp, .dbf, and .prj files, parses geographic features and attributes, and supports coordinate transformation using Proj4js. The result is a GeoJSON FeatureCollection, enabling easy integration with web mapping libraries.

目前為 2025-02-15 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/526996/1537647/GeoSHPer.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

作者
JS55CT
版本
1.0.0
建立日期
2025-02-15
更新日期
2025-02-15
尺寸
33.5 KB
授權條款
MIT

GeoSHPer

GeoSHPer is a JavaScript library designed to convert shapefile data into GeoJSON format effortlessly. It reads ZIP archives containing various components of shapefiles (.shp, .dbf, and .prj files), parses geographic features and attributes, and supports coordinate transformations using Proj4js. The final result is a GeoJSON FeatureCollection, which can be easily integrated with web mapping libraries.

Features

  • Convert Shapefile to GeoJSON: Reads compressed shapefile formats and outputs GeoJSON, a standard format used by mapping libraries.
  • Coordinate Transformation: Utilize Proj4js to handle complex map projections.
  • Support for Multi-layer Files: Automatically processes multiple shapefile layers within a single ZIP archive.
  • Parsing Support: Extracts data from .shp (geometry), .dbf (attributes), and .prj (projection information) files.

Installation Dependency

To use GeoSHPer, you need to include the Proj4js library as a dependency:

<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.15.0/proj4-src.js"></script>

or via Tamplermonkey / GreasyFork

// @require  https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.15.0/proj4-src.js

Usage

Basic Example

Here is a simple example of how to use GeoSHPer to convert shapefile data to GeoJSON:

(async () => {
    const response = await fetch('path/to/your/shapefile.zip');
    const buffer = await response.arrayBuffer();

    const geoSHPer = new GeoSHPer();
    await geoSHPer.read(buffer);
    const geoJSON = geoSHPer.toGeoJSON();
    console.log(geoJSON);
})();

Handling Different Encodings

GeoSHPer supports different string encoding formats specified in .cpg files for .dbf attributes.

// Assuming a `.cpg` file exists in your .zip providing character encoding details.

Error Handling

GeoSHPer provides error messages for common issues:

  • Forgot buffer: "forgot to pass buffer"
  • Invalid buffer: "invalid buffer like object"
  • Missing shapefile layers: "no layers found"

Ensure you handle these errors appropriately in your application.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Dependencies

  • Proj4js (MIT License) - For map projection transformations.

Acknowledgments

GeoSHPer builds upon foundational work from:

Each library is subject to its own license and must be used in accordance with their respective terms. This code adapts and extends functionalities from shpjs to convert shapefile data into GeoJSON.

Contact

Created by JS55CT - GitHub