JuliaGeo / LibGEOS.jl

Julia package for manipulation and analysis of planar geometric objects
MIT License
72 stars 24 forks source link

Convert from Shapefile.Polygon to Polygon #178

Closed nxiangg closed 1 year ago

nxiangg commented 1 year ago
Screen Shot 2023-07-21 at 12 49 46 PM

Hi,

I am completely new to this package. The ultimate goal is to get the centroid of each polygon. First, I read in a shapefile using Shapefile.jl. Second, I try to convert the shapefile.polygon to polygon so that I can use the centroid() function. However, I am getting the following error message:

ERROR: UndefVarError: of not defined
Stacktrace:
 [1] convert(t::Type{Polygon}, ::MultiPolygonTrait, geom::Shapefile.Polygon; context::Nothing)
   @ LibGEOS ~/.julia/packages/LibGEOS/oez93/src/geo_interface.jl:116
 [2] convert(t::Type{Polygon}, ::MultiPolygonTrait, geom::Shapefile.Polygon)
   @ LibGEOS ~/.julia/packages/LibGEOS/oez93/src/geo_interface.jl:116
 [3] convert(T::Type, geom::Shapefile.Polygon)
   @ GeoInterface ~/.julia/packages/GeoInterface/JDGLc/src/interface.jl:636
 [4] top-level scope
   @ ~/nbhd_optimization/code/3-optimize.jl:10

Any help or suggestion would be extremely helpful! Thank you very much!

jaakkor2 commented 1 year ago

Try

GeoInterface.convert(LibGEOS, table.geometry[1])

Note: best to show the code as text by putting it between two ```.

Note2: https://github.com/JuliaGeo/LibGEOS.jl/blob/v0.8.4/src/geo_interface.jl#L117 this line calls for function of which does not seem to be defined.

nxiangg commented 1 year ago

@jaakkor2 Thank you very much! First, let me put the code as text:

shp_path = "../data/large_locations_2022-06-08/Large Locations_8th June.shp"
table = Shapefile.Table(shp_path)
geoms = Shapefile.shapes(table)

typeof(table.geometry[1])
GeoInterface.convert(LibGEOS, table.geometry[1])

Unfortunately, this does not solve my problem. I got another error message:

ERROR: MethodError: no method matching ncoord(::LinearRingTrait, ::Shapefile.LinearRing{Shapefile.Point, Nothing, Nothing})
Closest candidates are:
  ncoord(::Union{CircularStringTrait, CompoundCurveTrait, CurvePolygonTrait, GeometryCollectionTrait, LineStringTrait, LinearRingTrait, MultiLineStringTrait, MultiPointTrait, MultiPolygonTrait, MultiSurfaceTrait, PointTrait, PolygonTrait, PolyhedralSurfaceTrait, TINTrait, TriangleTrait}, ::ArchGDAL.AbstractGeometry) at /Users/nxiang/.julia/packages/ArchGDAL/R3wJR/src/geointerface.jl:72
  ncoord(::AbstractGeometryTrait, ::LibGEOS.PreparedGeometry) at /Users/nxiang/.julia/packages/LibGEOS/oez93/src/geo_interface.jl:61
  ncoord(::AbstractGeometryTrait, ::LibGEOS.AbstractGeometry) at /Users/nxiang/.julia/packages/LibGEOS/oez93/src/geo_interface.jl:56
  ...
Stacktrace:
  [1] coordnames(t::LinearRingTrait, geom::Shapefile.LinearRing{Shapefile.Point, Nothing, Nothing})
    @ GeoInterface ~/.julia/packages/GeoInterface/JDGLc/src/fallbacks.jl:9
  [2] coordnames(geom::Shapefile.LinearRing{Shapefile.Point, Nothing, Nothing})
    @ GeoInterface ~/.julia/packages/GeoInterface/JDGLc/src/interface.jl:111
  [3] is3d(#unused#::LinearRingTrait, geom::Shapefile.LinearRing{Shapefile.Point, Nothing, Nothing})
    @ GeoInterface ~/.julia/packages/GeoInterface/JDGLc/src/fallbacks.jl:17
  [4] is3d(geom::Shapefile.LinearRing{Shapefile.Point, Nothing, Nothing})
    @ GeoInterface ~/.julia/packages/GeoInterface/JDGLc/src/interface.jl:611
  [5] _geom_to_coord_seq(geom::Shapefile.LinearRing{Shapefile.Point, Nothing, Nothing}, context::LibGEOS.GEOSContext)
    @ LibGEOS ~/.julia/packages/LibGEOS/oez93/src/geo_interface.jl:123
  [6] convert(::Type{LinearRing}, ::LinearRingTrait, geom::Shapefile.LinearRing{Shapefile.Point, Nothing, Nothing}; context::LibGEOS.GEOSContext)
    @ LibGEOS ~/.julia/packages/LibGEOS/oez93/src/geo_interface.jl:95
  [7] convert(::Type{Polygon}, ::PolygonTrait, geom::Shapefile.SubPolygon{Shapefile.LinearRing{Shapefile.Point, Nothing, Nothing}}; context::LibGEOS.GEOSContext)
    @ LibGEOS ~/.julia/packages/LibGEOS/oez93/src/geo_interface.jl:105
  [8] convert(::Type{MultiPolygon}, ::MultiPolygonTrait, geom::Shapefile.Polygon; context::LibGEOS.GEOSContext)
    @ LibGEOS ~/.julia/packages/LibGEOS/oez93/src/geo_interface.jl:111
  [9] convert(::Type{MultiPolygon}, ::MultiPolygonTrait, geom::Shapefile.Polygon)
    @ LibGEOS ~/.julia/packages/LibGEOS/oez93/src/geo_interface.jl:111
 [10] convert(package::Module, geom::Shapefile.Polygon)
    @ GeoInterface ~/.julia/packages/GeoInterface/JDGLc/src/fallbacks.jl:132
 [11] top-level scope
    @ ~/nbhd_optimization/code/3-optimize.jl:11
jaakkor2 commented 1 year ago

Can you show typeof(table.geometry[1]) and contents of table.geometry[1]?

Also, best to show the versions of the packages you are using. And try with the latest packages.

Can you share Large Locations_8th June.shp ?

nxiangg commented 1 year ago

Hi @jaakkor2 ,typeof(table.geometry[1]) is Shapefile.Polygon. Its content is:

6-element Vector{Shapefile.Point}:
 Shapefile.Point(36.786531398, -1.291551789)
 Shapefile.Point(36.790865848, -1.292581495)
 Shapefile.Point(36.791488121, -1.29644289)
 Shapefile.Point(36.786488483, -1.296657412)
 Shapefile.Point(36.785673092, -1.291530336)
 Shapefile.Point(36.786531398, -1.291551789)

I am using 0.8.1 version for LibGEOS and 0.9.2 for Shapefiles. You can download the shapefile in this link: [https://www.dropbox.com/sh/bobxfkbbwhewhkb/AAAMoe-wWELwcSVZdlnfX0dRa?dl=0] Thank you very much!

jaakkor2 commented 1 year ago

Works for me

julia> GeoInterface.convert(LibGEOS, table.geometry[1])
MULTIPOLYGON (((36.786531398 -1.291551789, 36.790865848 -1.292581495, 36.791488121 -1.29644289, 36.786488483 -1.296657412, 36.785673092 -1.291530336, 36.786531398 -1.291551789)))
  [c9ce4bd3] ArchGDAL v0.10.1
  [a93c6f00] DataFrames v1.6.0
⌃ [cf35fbd7] GeoInterface v1.3.0
  [5c1252a2] GeometryBasics v0.4.9
  [4d00f742] GeometryTypes v0.8.5
⌃ [a90b1aa1] LibGEOS v0.8.1
  [8e980c4a] Shapefile v0.10.0

Julia 1.9.2.

I do get the error you showed with ]add GeoInterface@1.3.0 Shapefile@0.9.2 ArchGDAL@0.10.1 LibGEOS@0.8.1.

nxiangg commented 1 year ago

Thank you so much! I solved this issue by updating my Shapefile.jl to v0.10.0. I will close this issue.