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

unknown geom type: reading (mixed) Unknown fails #1160

Closed
mdsumner opened this issue May 18, 2023 · 6 comments
Closed

unknown geom type: reading (mixed) Unknown fails #1160

mdsumner opened this issue May 18, 2023 · 6 comments

Comments

@mdsumner
Copy link
Contributor

vect() can't read polygon TAB, because (see link below)

the mapinfo driver deliberately reports an unknown geometry type for polygon files

terra::vect("/vsicurl/https://github.com/OSGeo/gdal/raw/master/autotest/ogr/data/mitab/poly_indexed.tab")
Error: [vect] cannot read this geometry type: Unknown (any)

## other interfaces work
##sf::read_sf("/vsicurl/https://github.com/OSGeo/gdal/raw/master/autotest/ogr/data/mitab/poly_indexed.tab")

https://lists.osgeo.org/pipermail/gdal-dev/2008-June/017418.html

Will PR if I can, found this exploring use of SQLITE spatial functions in query() which seem to also return mixed type.

dsn <- system.file("ex/lux.shp", package="terra")
layer <- "lux"
dialect <- "SQLITE"
geomname <- "ConvexHull(GEOMETRY) AS GEOMETRY"  
vrt <- glue::glue('<OGRVRTDataSource>
        <OGRVRTLayer name="polygons_as_hulls">
          <SrcDataSource>{dsn}</SrcDataSource>
          <SrcSQL dialect="{dialect}">SELECT NAME_1, {geomname} FROM {layer}</SrcSQL>
    </OGRVRTLayer>
</OGRVRTDataSource>')

terra::vect(vrt)
# Error: [vect] cannot read this geometry type: Unknown (any)
@mdsumner
Copy link
Contributor Author

possibly related to #1084 because

terra::vect("/vsicurl/https://filesamples.com/samples/code/kml/sample3.kml")
Error: [vect] cannot read this geometry type: Unknown (any)
In addition: Warning message:
[vect] Z coordinates ignored 

@rhijmans
Copy link
Member

The bigger problem in the context of "unknown" or "collection" types is that a SpatVector is assumed to have a single data type. Allowing multiple types requires a major overhaul. I will start by adding a method that can read these into a SpatVectorCollection; and then we can take it from there.

rhijmans added a commit that referenced this issue May 18, 2023
@rhijmans
Copy link
Member

rhijmans commented May 18, 2023

There now is a svc<character> method that can read MapInfo polygons

library(terra)
v <- vect(system.file("ex/lux.shp", package="terra"))
v$POP <- v$POP * 1
f <-  "mi_pols.tab"
writeVector(v,f)

x <- svc(f)
x[1]
# class       : SpatVector 
# geometry    : polygons 
# dimensions  : 12, 6  (geometries, attributes)
# extent      : 5.74414, 6.528252, 49.44781, 50.18162  (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=longlat +datum=WGS84 +no_defs 
# names       :  ID_1   NAME_1  ID_2   NAME_2  AREA       POP
# type        : <num>    <chr> <num>    <chr> <num>     <num>
# values      :     1 Diekirch     1 Clervaux   312 1.808e+04
#                   1 Diekirch     2 Diekirch   218 3.254e+04
#                   1 Diekirch     3  Redange   259 1.866e+04

@mdsumner
Copy link
Contributor Author

mdsumner commented May 18, 2023

good to know ty, I'll PR something for the documentation 🙏

@rhijmans
Copy link
Member

You can now also read polygon TAB with terra::vect

library(terra)
v <- vect(system.file("ex/lux.shp", package="terra"))
v$POP <- v$POP * 1
f <-  "mi_pols.tab"
writeVector(v,f, overwrite=TRUE)
x <- vect(f)
x
# class       : SpatVector 
# geometry    : polygons 
# dimensions  : 12, 6  (geometries, attributes)
# extent      : 5.74414, 6.528252, 49.44781, 50.18162  (xmin, xmax, ymin, ymax)
# source      : mi_pols.tab
# coord. ref. : +proj=longlat +datum=WGS84 +no_defs 
# names       :  ID_1   NAME_1  ID_2   NAME_2  AREA       POP
# type        : <num>    <chr> <num>    <chr> <num>     <num>
# values      :     1 Diekirch     1 Clervaux   312 1.808e+04
#                   1 Diekirch     2 Diekirch   218 3.254e+04
#                   1 Diekirch     3  Redange   259 1.866e+04

If there are multiple geometries in a file, the highest level is returned (polygons > lines > points), with a warning suggesting to use svc to get all geometry types.

@mdsumner
Copy link
Contributor Author

awesome, thanks so much, confirmed with all examples above

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

2 participants