Giter Site home page Giter Site logo

shapefile-rs's Introduction

shapefile-rs

Rust library to read & write shapefiles .dbf files supported via the dbase crate

let mut reader = shapefile::Reader::from_path(filename).unwrap();

for result in reader.iter_shapes_and_records() {
    let (shape, record) = result.unwrap();
    println ! ("Shape: {}, records: ", shape);
    for (name, value) in record {
        println ! ("\t{}: {:?}, ", name, value);
    }
    println ! ();
}

You can check out examples in the examples folder

shapefile-rs's People

Contributors

adamreichold avatar farkal avatar joxit avatar maximkaaa avatar michelk avatar pjsier avatar rfilmyer avatar sabrinajewson avatar tmontaigu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

shapefile-rs's Issues

Not able to read feature metadata.

I'm very new to Rust (somewhat experienced with GIS but it's not my main background), so apologies if there's something I just failed to grok properly.

I'm trying to read shapefiles with contour lines (from the Ordanance Survey Terr50 data FWIW), which are grouped into features with 2d coordinates and a 'PROP_VALUE' indicating the height of each line. I've previously been using https://github.com/calvinmetcalf/shapefile-js and wanted to experiment with porting that code to Rust / WASM.

In the context of shapefile.js, they parse the results into GeoJSON FeatureCollection, where each Feature has, in addition to geometry, a set of arbitrary properties indexed by string. So I access the height with feature.properties['PROP_VALUE'].

Reading through your documentation and code, I don't think I see anyway to do the equivalent with shapefile-rs, so I guess it's just a missing feature, but I could well have missed something.

Incorrect byte in index file (index records)

Hi, thank you for this project ๐Ÿ˜„

I'm using your project to create shapefiles and it seems that indexes are corrupted, I can't open the result shapefile with QGIS.

My test is this one :

use shapefile::record::poly::GenericPolygon;
use shapefile::*;
  #[test]
  pub fn test() {
    let poly: GenericPolygon<Point> = GenericPolygon::new(vec![
        Point::new(-2.109375, 54.57206165565852),
        Point::new(-11.953125, 31.952162238024975),
        Point::new(31.640625, 44.08758502824516),
        Point::new(-2.109375, 54.57206165565852),
    ]);

    Writer::from_path("/tmp/shp/test.shp")
      .unwrap()
      .write_shapes(vec![poly])
      .unwrap();
  }

The test as GeoJSON :

{
  "type": "Feature",
  "properties": {},
  "geometry": {
    "type": "Polygon",
    "coordinates": [
      [
        [ -2.109375, 54.57206165565852 ],
        [ -11.953125, 31.952162238024975 ],
        [ 31.640625, 44.08758502824516 ],
        [ -2.109375, 54.57206165565852 ]
      ]
    ]
  }
}

I used Qgis to transform the GeoJSON to a shapefile. I found that the QGIS index and Shapefile-rs index where different.

I did a hexyl (the rust version of hexdump) and here is the result :

hexyl shapefile-rs.shx
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚00000000โ”‚ 00 00 27 0a 00 00 00 00 โ”Š 00 00 00 00 00 00 00 00 โ”‚00'_0000โ”Š00000000โ”‚
โ”‚00000010โ”‚ 00 00 00 00 00 00 00 00 โ”Š 00 00 00 36 e8 03 00 00 โ”‚00000000โ”Š0006ร—โ€ข00โ”‚
โ”‚00000020โ”‚ 05 00 00 00 00 00 00 00 โ”Š 00 e8 27 c0 ae cd 88 e7 โ”‚โ€ข0000000โ”Š0ร—'ร—ร—ร—ร—ร—โ”‚
โ”‚00000030โ”‚ c0 f3 3f 40 00 00 00 00 โ”Š 00 a4 3f 40 ab 2c fb 50 โ”‚ร—ร—?@0000โ”Š0ร—?@ร—,ร—Pโ”‚
โ”‚00000040โ”‚ 39 49 4b 40 00 00 00 00 โ”Š 00 00 00 00 00 00 00 00 โ”‚9IK@0000โ”Š00000000โ”‚
โ”‚00000050โ”‚ 00 00 00 00 00 00 00 00 โ”Š 00 00 00 00 00 00 00 00 โ”‚00000000โ”Š00000000โ”‚
โ”‚00000060โ”‚ 00 00 00 00 00 00 00 64 โ”Š 00 00 00 38             โ”‚0000000dโ”Š0008    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

hexyl qgis.shx 
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚00000000โ”‚ 00 00 27 0a 00 00 00 00 โ”Š 00 00 00 00 00 00 00 00 โ”‚00'_0000โ”Š00000000โ”‚
โ”‚00000010โ”‚ 00 00 00 00 00 00 00 00 โ”Š 00 00 00 36 e8 03 00 00 โ”‚00000000โ”Š0006ร—โ€ข00โ”‚
โ”‚00000020โ”‚ 05 00 00 00 00 00 00 00 โ”Š 00 e8 27 c0 ae cd 88 e7 โ”‚โ€ข0000000โ”Š0ร—'ร—ร—ร—ร—ร—โ”‚
โ”‚00000030โ”‚ c0 f3 3f 40 00 00 00 00 โ”Š 00 a4 3f 40 ab 2c fb 50 โ”‚ร—ร—?@0000โ”Š0ร—?@ร—,ร—Pโ”‚
โ”‚00000040โ”‚ 39 49 4b 40 00 00 00 00 โ”Š 00 00 00 00 00 00 00 00 โ”‚9IK@0000โ”Š00000000โ”‚
โ”‚00000050โ”‚ 00 00 00 00 00 00 00 00 โ”Š 00 00 00 00 00 00 00 00 โ”‚00000000โ”Š00000000โ”‚
โ”‚00000060โ”‚ 00 00 00 00 00 00 00 32 โ”Š 00 00 00 38             โ”‚00000002โ”Š0008    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The first 100 bytes are correct (the index header) but the 8 last bytes are different, we see 64 with this project and 32 with qgis.

When I replace shapefile-rs.shx by qgis.shx it works like a charm, tha'ts why I think the issue is in the index.

Trying to port to dbase 0.1 - ownership issue on Reader::iter_shapes_and_records_as

Hi,

I was trying to see if I could get this crate to work with the newest version of another one of your crates, dbase 0.1.

Although the API has changed I think I was able to figure out how to switch from a dbase::Writer to a dbase::TableWriter.

Unfortunately, I seem to be stuck dealing with some ownership issues - since dbase::Reader isn't Iterator anymore, I had to rewrite iter_shapes_and_records_as to return a dbase::RecordIterator, but I can't figure out how to give something ownership of the dbase::Reader

I wrote my function something like this.

    pub fn iter_shapes_and_records_as<'a, S: ReadableShape>(
        mut self,
    ) -> Result<ShapeRecordIterator<'a, T, S>, Error> {
        let mut dbf_reader = self.dbf_reader.take().ok_or(Error::MissingDbf)?; // Now dbf_reader is owned by the current function
        let dbf_iter = dbf_reader.iter_records(); // but a dbase::RecordIterator only has a reference to it: `&'a mut Reader<T>`
        Ok(ShapeRecordIterator{
            shape_iter: self.iter_shapes_as::<S>(),
            dbf_iter, // thus this is invalid because it "returns a value referencing data owned by the current function"
        })

    }

Sorry if this issue was invalid but I can't figure out of this is an issue with dbase, or if I'm just not writing something correctly.

convert to geotype ?

Hey,

Thanks for the great parsing but i would like to convert your object to geotype.

I try with try_into but doesn't work. Have you any idea on how to do this ?

attempt to multiply with overflow with hdr.record_size in read_one_shape_as

Hello,

I am a beginner in rust and shape file in general.

When i open shape, shapefile panic :
'main' panicked at 'attempt to multiply with overflow', ...shapefile-0.3.0\src\reader.rs:107:23.

My code (simplify) :

fn open_shape(filename: String) -> Vec<(shapefile::Shape, dbase::Record)>{
    let mut reader;
    match shapefile::Reader::from_path(filename.to_string()){
        Ok(x) => reader = x,
        Err(shapefile::Error::MissingDbf) => exit(2),
        _ => exit(3)
    };
    match reader.read() {
        Ok(data_shape) => return data_shape,
        Err(e) => exit(4)
    }   
}

When i debug i see this :

panic in reader.read() in read_one_shape_as
this line : reader.rs 107 : let record_size = hdr.record_size * 2;
hdr.record_size = -1409209791

I don't if my shape have a problem or if it's an error not properly catch by the library.
i have no problem to open my shape in qgis

Provide better shape constructors

Current constructors for the shapes are error-prone

The current form new(points: Vec<PointType>, parts: Vec<i32>) requires that users properly organize the points in the Vec and that parts array is properly populated.
As no real checks are done, this can lead to wrong shapefile being written.

We should change constructors to something like:

new(points: Vec<PointType>)
with_multiparts(points: Vec<Vec<PointType>>)

Constructor could then sort points clock-wise/counter clock-wise to match the ESRI standard
and generate the parts array preventing from mistakes.

And maybe we could add Builders.

Character encoding

read shapefile can be assign "utf-8","gbk" for better read charactor code

Shape: Shape::Polyline(1 parts), records:
former_nam: Character(None),
Route_ID: Character(Some("99564")),
Path_Name: Character(Some("๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ")),

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.