Ebook_Efficient Geospatial Data Management_geoeasy (1)
Ebook_Efficient Geospatial Data Management_geoeasy (1)
By Ronaldo Menezes
2024
Sumário
What is OSGeo4W64? 1. GDAL/OGR with Integration
03 09
into R (using system())
Selecting Packages: In the advanced installation, you can choose which packages you
want to install, such as GDAL, QGIS, or any other tools of interest.
Daily Usage: After installation, the installed tools can be accessed through the Windows
"Start" menu or directly via the command terminal, where you can execute commands
like gdalinfo, ogr2ogr, or open QGIS.
3
Updates and Maintenance: OSGeo4W64 makes it easy to update the installed
packages. You can simply rerun the installer and select "Advanced Install" to check for
available updates.
OSGeo4W64 is a powerful and comprehensive solution for those working with geospatial
data on Windows. It centralizes the installation and management of essential tools,
supports a wide range of formats and integrations, and is supported by an active
community. If you work with geoprocessing, geospatial data visualization, or map
publishing, OSGeo4W64 is an indispensable tool for your workflow.
4
Introduction to GDAL/OGR with R Integration and SQLite
using GeoPackage
To start working with GDAL/OGR, integration with R, and using GeoPackage via SQLite,
installing OSGeo4W is essential. OSGeo4W is a collection of open-source packages that
contain tools like GDAL/OGR, QGIS, GRASS GIS, and others.
adopted relational database, GeoPackage allows storing vectors, rasters, attribute tables,
and metadata all within a single compact and portable file. This capability to store
different types of data in a single file makes GeoPackage an ideal choice for projects
requiring the integration of multiple sources and types of geospatial data.
5
In addition to its storage capabilities, GeoPackage supports a full range of geospatial
operations, allowing users to perform complex spatial queries, geometry manipulations,
and raster analyses directly in the database. With native support in widely used tools
such as QGIS, GDAL/OGR, and various programming languages like Python and R,
GeoPackage offers a level of interoperability and flexibility that few formats can match.
install.packages("sf")
install.packages("RSQLite")
library(sf)
library(RSQLite)
Working with GeoPackage in R: You can load and manipulate GeoPackage data using
the sf package, which is modern and streamlined.
# Load a GeoPackage
gpkg_path <- "path/to/your/file.gpkg"
layer <- st_read(gpkg_path, layer = "layer_name")
First, open a connection to the GeoPackage file as you would with a regular SQLite
database.
library(DBI)
library(RSQLite)
# Connect to GeoPackage
con <- dbConnect(RSQLite::SQLite(), "path/to/your/file.gpkg")
6
Executing SQL Queries on a GeoPackage:
Now that you are connected to the GeoPackage, you can execute SQL queries
directly on it.
After finishing your operations, remember to close the connection to the database.
With this setup, you can combine the powerful geospatial capabilities of GDAL/OGR and
the flexible database management system of SQLite to handle complex geospatial
workflows in R.
library(sf)
7
2. Using RSQLite for SQL Queries on a GeoPackage
RSQLite can be used to access non-spatial tables within the GeoPackage or to perform
SQL operations directly on the underlying database. This can be useful for analyses that
do not involve spatial operations.
library(DBI)
library(RSQLite)
Combined Example Here’s an example of how you might use both packages in a
workflow:
1. Load a spatial layer with sf for visualization and manipulation.
2. Use RSQLite to extract or manipulate data from non-spatial tables or perform SQL
queries.
3. Write back the manipulated data with sf to a new GeoPackage.
library(sf)
library(RSQLite)
9
These examples illustrate how to integrate GDAL/OGR with R using system(), execute
commands directly in the terminal, and manipulate geospatial data in R using
GeoPackage via SQLite. These practices are essential for efficient and automated
geoprocessing workflows in GIS environments.
library(sf)
10
Example 2: Executing SQL Queries on a GeoPackage
Using RSQLite, we execute SQL queries directly on a GeoPackage.
library(RSQLite)
library(DBI)
library(sf)
library(RSQLite)
library(DBI)
11