Giter VIP home page Giter VIP logo

shapefilereader's Introduction

ShapefileReader

A shapefile reader in Swift

Description

Reads data from files in Shapefile format.

API Overview

ShapefileReader is the only object you instantiate.

Internally, it builds the three following objects, depending on the available auxiliary files:

  • SHPReader to read the shapes, ie the list of points
  • DBFReader to read the data records associated with the shapes
  • SHXReader to read the indices of the shapes, thus allowing direct access
  • PRJReader to read the coordinate system and projection information

Most usages of ShapefileReader will remain like:

guard let sr = ShapefileReader(path: "g1g15.shp") else { assertionFailure() }

// iterate over both shapes and records
for (shape, record) in zip(sr.shp, sr.dbf!) {
    // record is [AnyObject]

    for points in shape {
        // draw polygon with [CGPoint]
        // or map to [CLLocationCoordinate2D] using sr.coordinateConverter()
    }
}

DBF API

// if dbf file exists
if let dbf = sr.dbf {
    // number of records
    let n = dbf.numberOfRecords
    
    // fields description
    print(dbf.fields)
    
    // iterate over records
    for r in dbf {
        // ...
    }
    
    // access a record by index
    print(dbf[1847])
}
[[DeletionFlag, C, 1, 0], [GMDNR, N, 9, 0], [GMDNAME, C, 50, 0], [BZNR, N, 9, 0], [KTNR, N, 9, 0], [GRNR, N, 9, 0], [AREA_HA, N, 9, 0], [X_MIN, N, 9, 0], [X_MAX, N, 9, 0], [Y_MIN, N, 9, 0], [Y_MAX, N, 9, 0], [X_CNTR, N, 9, 0], [Y_CNTR, N, 9, 0], [Z_MIN, N, 9, 0], [Z_MAX, N, 9, 0], [Z_AVG, N, 9, 0], [Z_MED, N, 9, 0], [Z_CNTR, N, 9, 0]]  
[5586, Lausanne, 2225, 22, 1, 4138, 534438, 544978, 150655, 161554, 538200, 152400, 371, 930, 670, 666, 585]

SHX API

// if index file exists
if let shx = sr.shx {
    // access a shape by index
    if let offset = shx.shapeOffsetAtIndex(2) {
    	if let (nextIndex, shape) = shp.shapeAtOffset(offset) {
	        // use shape   	
    	}
    }
}

or more simply

let shape = sr[2]

PRJ API

if let cs = sr.prj?.cs {
    // use CoordinateSystem information to convert CGPoint to CLLocationCoordinate2D
}

You can override sr.coordinateConverter() to support coordinate systems other than WGS84

Implementation Details

  • shape points are CGPoint arrays
  • record are arrays of Int, Double, Bool or String (no NSDate, no optionals)
  • random access in files and enumerators are used each time it is possible
  • most of time, code will ignore when files do not match the specs, feel free to open an issue and join the offending files in case of crash

Tests and Drawing

The package comes with a unit test target.

Also, it comes with a project that contains BitmapCanvas and its subclass BitmapCanvasShapefile which will generate the following PNG files.

Switzerland Altitude

Switzerland ZIP Codes

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.