hypertidy / dsn

Data Source Name Helpers for Use With GDAL
https://hypertidy.github.io/dsn/
Other
6 stars 0 forks source link

xml2 approach for VRT #8

Open mdsumner opened 10 months ago

mdsumner commented 10 months ago

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 commented 10 months ago

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