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

xml2 approach for VRT #8

Open
mdsumner opened this issue Jan 3, 2024 · 1 comment
Open

xml2 approach for VRT #8

mdsumner opened this issue Jan 3, 2024 · 1 comment

Comments

@mdsumner
Copy link
Member

mdsumner commented Jan 3, 2024

having trouble with the ST_funs for SQLITE dialect ...

library(xml2)

schema <- read_xml("https://raw.githubusercontent.com/OSGeo/gdal/master/data/ogrvrt.xsd")
xml <- xml_new_root("OGRVRTDataSource")
xml_add_child(xml, "OGRVRTLayer", name = "CGAZ_query")
xml_add_child(tail(xml_children(xml), 1), "SrcDataSource", sds::CGAZ())
#xml_add_child(tail(xml_children(xml), 1), "SrcSQL", "SELECT ST_Simplify(geometry) FROM geoBoundariesCGAZ_ADM0 WHERE shapeGroup IN ('AUS')", dialect = "SQLITE")
#xml_add_child(tail(xml_children(xml), 1), "SrcSQL", "SELECT ST_Centroid(ogr_geocode('Paris'))", dialect = "SQLITE")


xml_validate(xml, schema)

write_xml(xml, con, options = "no_declaration")
dsn <- readr::read_file(con)

vapour::vapour_read_geometry(dsn)

@mdsumner
Copy link
Member Author

mdsumner commented Jan 3, 2024

bit more of an example with streaming reprojection

'<OGRVRTDataSource>
    <OGRVRTWarpedLayer>
        <OGRVRTLayer name="source">
            <SrcDataSource>source.shp</SrcDataSource>
        </OGRVRTLayer>
        <TargetSRS>EPSG:4326</TargetSRS>
    </OGRVRTWarpedLayer>
</OGRVRTDataSource>'
  

library(xml2)

target_crs <- NULL
target_crs <- "+proj=laea"

schema <- read_xml("https://raw.githubusercontent.com/OSGeo/gdal/master/data/ogrvrt.xsd")
xml <- xml_new_root("OGRVRTDataSource")
if (!is.null(target_crs)) {
 xml_add_child(xml, "OGRVRTWarpedLayer")
 
}
xml_add_child(tail(xml_children(xml), 1), "OGRVRTLayer", name = "CGAZ_query")
xml_add_child(xml_children(xml_children(xml)), "SrcDataSource", sds::CGAZ())
xml_add_child(xml_children(xml_children(xml)), "SrcSQL", "SELECT * FROM geoBoundariesCGAZ_ADM0", dialect = "SQLITE")

xml_add_child(tail(xml_children(xml), 1), "TargetSRS", target_crs)

xml_validate(xml, schema)

write_xml(xml, "/tmp/file.vrt", options = "no_declaration")
# 
# #dsn <- "<OGRVRTDataSource><OGRVRTWarpedLayer><OGRVRTLayer name=\"CGAZ_query\"/><TargetSRS>+proj=laea</TargetSRS><SrcSQL>SELECT * FROM geoBoundariesCGAZ_ADM0 LIMIT 1</SrcSQL></OGRVRTWarpedLayer></OGRVRTDataSource>\n<SrcDataSource>/vsizip//vsicurl/https://github.com/wmgeolab/geoBoundaries/raw/main/releaseData/CGAZ/geoBoundariesCGAZ_ADM0.zip</SrcDataSource"
 dsn <- trimws(readr::read_file("/tmp/file.vrt"))
plot(wk::wkb( vapour::vapour_read_geometry("/tmp/file.vrt")))
# 

image

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

1 participant