yeesian / ArchGDAL.jl

A high level API for GDAL - Geospatial Data Abstraction Library
https://yeesian.github.io/ArchGDAL.jl/stable/
Other
142 stars 26 forks source link

Added a new method to `AG.getlayer` without index or name arg #257

Closed mathieu17g closed 3 years ago

mathieu17g commented 3 years ago

Fixes #256

Added:

Cleaned some remaining mentions of Table in getlayer methods

julia> AG.read("test/data/point.geojson") do ds
           layer = AG.getlayer(ds)
           println(layer)
           new_ds = AG.copy(layer; name="duplicated layer 1").ownedby
           AG.copy(layer; dataset = new_ds, name="duplicated layer 2")
           println("Number of layers: $(AG.nlayer(new_ds))")
           println(AG.getlayer(new_ds))
       end
Layer: point
  Geometry 0 (): [wkbPoint], POINT (100 0), POINT (100.2785 0.0893), ...
     Field 0 (FID): [OFTReal], 2.0, 3.0, 0.0, 3.0
     Field 1 (pointname): [OFTString], point-a, point-b, a, b

Number of layers: 2
┌ Warning: Dataset has multiple layers, getting the first layer
└ @ ArchGDAL ~/.../dev/ArchGDAL/src/dataset.jl:529
Layer: duplicated layer 1
  Geometry 0 (): [wkbPoint], POINT (100 0), POINT (100.2785 0.0893), ...
     Field 0 (FID): [OFTReal], 2.0, 3.0, 0.0, 3.0
     Field 1 (pointname): [OFTString], point-a, point-b, a, b
mathieu17g commented 3 years ago

With unsafe_getlayer and the additional hint in warning message, it gives:

julia> AG.read("test/data/point.geojson") do ds
           layer = AG.getlayer(ds)
           println(layer)
           new_ds = AG.copy(layer; name="duplicated layer 1").ownedby
           AG.copy(layer; dataset = new_ds, name="duplicated layer 2")
           println("Number of layers: $(AG.nlayer(new_ds))")
           AG.getlayer(new_ds) do new_layer
               println(new_layer)
           end
       end
Layer: point
  Geometry 0 (): [wkbPoint], POINT (100 0), POINT (100.2785 0.0893), ...
     Field 0 (FID): [OFTReal], 2.0, 3.0, 0.0, 3.0
     Field 1 (pointname): [OFTString], point-a, point-b, a, b

Number of layers: 2
┌ Warning: Dataset has multiple layers, getting the first layer
│ Specify the layer number or name to avoid the warning
└ @ ArchGDAL ~/.../dev/ArchGDAL/src/dataset.jl:542
Layer: duplicated layer 1
  Geometry 0 (): [wkbPoint], POINT (100 0), POINT (100.2785 0.0893), ...
     Field 0 (FID): [OFTReal], 2.0, 3.0, 0.0, 3.0
     Field 1 (pointname): [OFTString], point-a, point-b, a, b
visr commented 3 years ago

Nice, thanks! Personally I prefer to turn the warning into an error here, like I mentioned in https://github.com/evetion/GeoDataFrames.jl/issues/15, but if others disagree, this is still a nice improvement.

yeesian commented 3 years ago

Yeah my preference is also to return an error if the dataset has more than one layer.

mathieu17g commented 3 years ago

Now we have an error

julia> AG.read("test/data/point.geojson") do ds
           layer = AG.getlayer(ds)
           println(layer)
           new_ds = AG.copy(layer; name="duplicated layer 1").ownedby
           AG.copy(layer; dataset = new_ds, name="duplicated layer 2")
           println("Number of layers: $(AG.nlayer(new_ds))")
           AG.getlayer(new_ds) do new_layer
               println(new_layer)
           end
       end
Layer: point
  Geometry 0 (): [wkbPoint], POINT (100 0), POINT (100.2785 0.0893), ...
     Field 0 (FID): [OFTReal], 2.0, 3.0, 0.0, 3.0
     Field 1 (pointname): [OFTString], point-a, point-b, a, b

Number of layers: 2
ERROR: Dataset has multiple layers. Specify the layer number or name
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] unsafe_getlayer(dataset::ArchGDAL.IDataset)
   @ ArchGDAL ~/.../dev/ArchGDAL/src/dataset.jl:541
 [3] getlayer(f::var"#84#86", args::ArchGDAL.IDataset; kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ ArchGDAL ~/.../dev/ArchGDAL/src/context.jl:265
 [4] getlayer(f::Function, args::ArchGDAL.IDataset)
   @ ArchGDAL ~/.../dev/ArchGDAL/src/context.jl:265
 [5] (::var"#83#85")(ds::ArchGDAL.Dataset)
   @ Main ./REPL[18]:7
 [6] read(f::var"#83#85", args::String; kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ ArchGDAL ~/.../dev/ArchGDAL/src/context.jl:267
 [7] read(f::Function, args::String)
   @ ArchGDAL ~/.../dev/ArchGDAL/src/context.jl:265
 [8] top-level scope
   @ REPL[18]:1