Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation for basic usage of geozero-shp #86

Open
jose-lpa opened this issue Dec 4, 2022 · 9 comments
Open

Documentation for basic usage of geozero-shp #86

jose-lpa opened this issue Dec 4, 2022 · 9 comments

Comments

@jose-lpa
Copy link

jose-lpa commented Dec 4, 2022

Hello, and thank you for this project.

I am trying to do a simple newbie program that, given an ESRI Shapefile as input, it outputs the geometries as GeoJSON.

I have been trying to use the geozero-shp crate, simply following the short example that can be found on its README, without any luck. It seems the example is incorrect, or it might have been outdated by outer changes on its dependencies.

I wrote this program:

use geozero::geojson::GeoJsonWriter;


fn main() {
    let path = "/home/jose/Downloads/ne_10m_admin_0_sovereignty/ne_10m_admin_0_sovereignty.shp";
    let reader = geozero_shp::Reader::from_path(path).unwrap();
    let mut json: Vec<u8> = Vec::new();
    let data = reader.iter_features(GeoJsonWriter::new(&mut json)).unwrap();
}

and when I run it, it doesn't seem that GeoJsonWriter is a valid thing to pass to the processor argument:

jose@uranium ~/C/e/shp_ingestor (master) [101]> cargo run -- /home/jose/Downloads/ne_10m_admin_0_sovereignty/ne_10m_admin_0_sovereignty.shp
   Compiling shp_ingestor v0.1.0 (/home/jose/Code/experimental/shp_ingestor)
error[E0277]: the trait bound `GeoJsonWriter<'_, Vec<u8>>: geozero::feature_processor::FeatureProcessor` is not satisfied
   --> src/main.rs:8:37
    |
8   |     let data = reader.iter_features(GeoJsonWriter::new(&mut json)).unwrap();
    |                       ------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `geozero::feature_processor::FeatureProcessor` is not implemented for `GeoJsonWriter<'_, Vec<u8>>`
    |                       |
    |                       required by a bound introduced by this call
    |
    = help: the following other types implement trait `geozero::feature_processor::FeatureProcessor`:
              geozero::ProcessorSink
              geozero::multiplex::Multiplexer<P1, P2>
note: required by a bound in `Reader::<T>::iter_features`
   --> /home/jose/.cargo/registry/src/github.com-1ecc6299db9ec823/geozero-shp-0.3.1/src/reader.rs:161:29
    |
161 |     pub fn iter_features<P: FeatureProcessor>(
    |                             ^^^^^^^^^^^^^^^^ required by this bound in `Reader::<T>::iter_features`

error[E0277]: the trait bound `GeoJsonWriter<'_, Vec<u8>>: geozero::feature_processor::FeatureProcessor` is not satisfied
  --> src/main.rs:8:16
   |
8  |     let data = reader.iter_features(GeoJsonWriter::new(&mut json)).unwrap();
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `geozero::feature_processor::FeatureProcessor` is not implemented for `GeoJsonWriter<'_, Vec<u8>>`
   |
   = help: the following other types implement trait `geozero::feature_processor::FeatureProcessor`:
             geozero::ProcessorSink
             geozero::multiplex::Multiplexer<P1, P2>
note: required by a bound in `ShapeRecordIterator`
  --> /home/jose/.cargo/registry/src/github.com-1ecc6299db9ec823/geozero-shp-0.3.1/src/reader.rs:39:35
   |
39 | pub struct ShapeRecordIterator<P: FeatureProcessor, T: Read + Seek> {
   |                                   ^^^^^^^^^^^^^^^^ required by this bound in `ShapeRecordIterator`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `shp_ingestor` due to 2 previous errors

Is there any documentation or code examples where I can see how this crate is used?

Thank you.

@michaelkirk
Copy link
Member

I'm going to move your issue to the geozero repository, since it seems to be about geozero.

@michaelkirk michaelkirk transferred this issue from georust/geojson Dec 6, 2022
@jose-lpa
Copy link
Author

jose-lpa commented Dec 6, 2022

It is indeed about geozero-shp. I had multiple tabs opened with Rust georust repos and I opened the issue in the wrong one 🤦‍♂️ Apologies for the mistake, and thank you very much for moving the issue here @michaelkirk

@pka
Copy link
Member

pka commented Jan 27, 2023

There is a test for converting Shp to GeoJSON:

fn shp_to_json() -> Result<(), geozero_shp::Error> {

@emirror-de
Copy link

emirror-de commented Oct 15, 2023

Hey, are there any updates on this? I created a new project, copied the test you mentioned in there and renamed it to fn main. It results in the same error message.

However, the tests are passing and if I use the test as example in the geozero-shp crate it also works.

To reproduce, create a new binary with:

Cargo.toml

[package]
name = "geozero-test"
version = "0.1.0"
edition = "2021"

[dependencies]
geozero-shp = "0.4"
geozero = "0.11"

src/main.rs

use geozero::geojson::GeoJsonWriter;
use std::str::from_utf8;

fn main() -> Result<(), geozero_shp::Error> {
    let reader = geozero_shp::Reader::from_path("./tests/data/poly.shp")?;
    let mut json: Vec<u8> = Vec::new();
    let cnt = reader
        .iter_features(&mut GeoJsonWriter::new(&mut json))?
        .count();
    assert_eq!(cnt, 10);
    assert_eq!(
        &from_utf8(&json).unwrap()[0..80],
        r#"{
"type": "FeatureCollection",
"features": [{"type": "Feature", "properties": {""#
    );
    assert_eq!(
        &from_utf8(&json).unwrap()[json.len()-100..],
        "2],[479658.59375,4764670],[479640.09375,4764721],[479735.90625,4764752],[479750.6875,4764702]]]]}}]}"
    );
    Ok(())
}

Edit: Using Rust 1.74

@nyurik
Copy link
Member

nyurik commented Oct 15, 2023

I just tried it, this is indeed really weird! Debugging... but if anyone has any ideas, please post

@nyurik
Copy link
Member

nyurik commented Oct 15, 2023

Ah, silly me - I think you are copy/pasting test code from a development (unpublished) version. When I added this to cargo.toml, it worked (I have geozero cloned in a parallel dir). I think all this was done by @michaelkirk in bee32bc

[patch.crates-io]
geozero = { path = "../../geozero/geozero" }
geozero-shp = { path = "../../geozero/geozero-shp" }

@emirror-de
Copy link

Ah that makes sense, I did not work with patching yet. Thanks for the fast reponse!

@nyurik
Copy link
Member

nyurik commented Oct 16, 2023

Just don't use it for any production code :) Patching is for experiments, and for when you have multiple crates in the same repo that depend on one another, and you need to modify them at the same time.

@emirror-de
Copy link

Ok nice to know. I am now using { path = "" } instead of the version number until a new release will be uploaded 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants