0% found this document useful (0 votes)
18 views

Ebook_Efficient Geospatial Data Management_geoeasy (1)

Uploaded by

Root Phone
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Ebook_Efficient Geospatial Data Management_geoeasy (1)

Uploaded by

Root Phone
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

GEOEASY

Efficient Geospatial Data


Management: Integrating
GDAL/OGR and SQLite
with R for Advanced GIS
Workflows

By Ronaldo Menezes
2024
Sumário
What is OSGeo4W64? 1. GDAL/OGR with Integration
03 09
into R (using system())

Introduction to GDAL/OGR with 2. GDAL/OGR in


05 10
R Integration and SQLite using Command Line (cmd)
GeoPackage

Advanced Geospatial Data 3. SQLite Using GeoPackage


05 10
Handling with GeoPackage: in R
Command Line, SQLite, and QGIS

Working with GeoPackage


07
using sf and RSQLite

Integrating sf and RSQLite


08
What is OSGeo4W64?
OSGeo4W64 is a package installer developed by the Open Source Geospatial
Foundation (OSGeo) for Windows systems. It simplifies the installation and management
of various open-source geospatial software tools and libraries. The "W" in OSGeo4W
indicates that it is designed for the Windows platform, and the "64" version refers to the
support for 64-bit operating systems, which are predominant in modern computers.

What is Included in OSGeo4W64?


OSGeo4W64 offers a wide range of packages, including:
GDAL/OGR:
GDAL (Geospatial Data Abstraction Library): A powerful library for reading and
writing various geospatial data formats, such as raster (images) and vector
(shapefiles, GeoJSON, etc.).
OGR: Part of GDAL, specifically for handling vector data. It allows reading and
writing formats such as Shapefile, GeoJSON, GeoPackage, among others.
QGIS: One of the most popular open-source GIS (Geographic Information Systems)
in the world. QGIS allows for visualization, editing, and analysis of geospatial data.
GRASS GIS: A highly advanced GIS for geospatial analysis. GRASS GIS is known
for its robust raster modeling and analysis capabilities.
Proj: A library for coordinate transformation and map projections. Essential for
ensuring that geospatial data is correctly visualized in different coordinate systems.
MapServer: A mapping engine that enables the publication of maps and geospatial
data on the web. It’s a powerful tool for creating interactive mapping applications.
SQLite and SpatiaLite:
SQLite: A lightweight, self-contained database system that doesn’t require a
separate server.
SpatiaLite: An extension of SQLite that adds support for geospatial data,
allowing SQLite to be used as a geospatial database.
Python: Although you mentioned that we are not using Python, it’s important to note
that OSGeo4W64 also provides Python bindings for libraries like GDAL, which is
useful for developers using Python for geospatial automation and scripting.

How to Use OSGeo4W64?


Installing OSGeo4W64: You can download the installer from the official OSGeo4W
website. During installation, you can choose between "Express Install" (pre-selected
package installation) or "Advanced Install" (custom package selection).

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.

Why Use OSGeo4W64?


Centralization of Tools: Instead of manually downloading and installing various
geospatial tools, OSGeo4W64 offers a single point for installation and updates.
Wide Format Support: With packages like GDAL/OGR, you have support for a wide
range of geospatial data formats, making it ideal for complex projects involving
multiple types of data.
Integration with GIS and Map Servers: Tools like QGIS and MapServer seamlessly
integrate with other available packages, making it easier to create complete
geospatial workflows.
Ease of Updates and Maintenance: The ability to manage all software packages in
one place makes tool maintenance and updates simpler and more efficient.
Large Community and Support: As it’s a widely used platform, there is a large
community of users and developers offering support and contributing with updates
and improvements.

Importance of the Packages Contained in OSGeo4W64


The packages available in OSGeo4W64 are essential for professionals and researchers
working with geospatial data. They offer:
Flexibility: Support for various data formats and integrations.
Analytical Power: Tools like GDAL and GRASS GIS offer advanced analytical
capabilities that are essential in geoprocessing studies.
Map Publishing: Packages like MapServer allow for the publication of data and
maps on the web, democratizing access to geospatial information.
Complete Geographic Information Systems: QGIS is a complete GIS that can be
used by both beginners and advanced professionals.

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.

Step-by-Step Guide to Installing OSGeo4W64


1. Download the OSGeo4W64 Installer:
Visit the official OSGeo4W website.
Click "Download" and choose the 64-bit version (OSGeo4W64). This is important
because most modern systems are 64-bit.
2. Run the Installer:
After downloading, run the installer (osgeo4w-setup-x86_64.exe).
Select the "Advanced Install" option for full control over the packages you want to
install.
3. Selecting Packages:
GDAL/OGR: In the installer, under "Commandline_Utilities," select the gdal
package (includes OGR). This will install the GDAL/OGR command-line utilities.
SQLite and SpatiaLite: In the "Libs" section, select sqlite3 and libspatialite for
SQLite and SpatiaLite support, which is an extension for SQLite that adds spatial
data support.
GeoPackage: GeoPackage is natively supported by GDAL, so you just need to
ensure GDAL is installed.
4. Completing the Installation:
After selecting the desired packages, click "Next" and wait for the installation to
complete.
OSGeo4W will install the packages and configure the necessary environment
variables.

Advanced Geospatial Data Handling with GeoPackage:


Command Line, SQLite, and QGIS
Introduction
1.1. What is GeoPackage?
In the world of Geographic Information Systems (GIS), the management and analysis of
large volumes of spatial data have been a constant need. Traditionally, formats like
Shapefile dominated the scene, offering a relatively simple way to store and share
geospatial data. However, as demands for efficiency, interoperability, and flexibility
increased, significant limitations arose with these traditional formats. It is in this context
that GeoPackage (GPKG) stands out as a modern, robust, and versatile solution.

GeoPackage is a geospatial database format developed by the Open Geospatial


Consortium (OGC) that combines the advantages of a relational database with the ability
to store and manipulate geospatial data. Built on the architecture of SQLite, a lightweight
and widely

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.

Setting Up R to Use GDAL/OGR, GeoPackage, and SQLite


Now that GDAL/OGR is installed, we can integrate it with R:

Installing the Necessary Packages in R: In R, install the sf and RSQLite packages,


which are necessary for working with spatial data and GeoPackage.

install.packages("sf")
install.packages("RSQLite")

Configuring R to Use GDAL/OGR: The sf package bridges R and GDAL/OGR. When


loading the package, it will use the GDAL libraries installed on your system.

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")

# Manipulate spatial data


summary(layer)
plot(layer)

# Save changes to a new GeoPackage


st_write(layer, "path/to/new_file.gpkg", layer = "new_layer")

Using RSQLite with GeoPackage


GeoPackage is essentially an SQLite database with geospatial support. Using the
RSQLite package, you can directly interact with the contents of the GeoPackage.

Connecting to a GeoPackage with RSQLite:

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.

# List tables (layers) in the GeoPackage


dbListTables(con)

# Query a specific layer


query <- dbGetQuery(con, "SELECT * FROM layer_name LIMIT 5")
print(query)

Closing the Connection:

After finishing your operations, remember to close the connection to the database.

# Disconnect when done


dbDisconnect(con)

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.

Working with GeoPackage using sf and RSQLite


To work with GeoPackage files in R, it's crucial to understand that the sf package is the
primary tool for handling spatial data. On the other hand, RSQLite can be used for simple
SQL queries but is not directly used to load the GeoPackage into sf. Let's explore how to
work with these two packages.

1. Loading and Manipulating a GeoPackage with sf


The sf package is used to load spatial layers from a GeoPackage and allows for spatial
operations directly in R.

library(sf)

# Load a layer from a GeoPackage


gpkg_path <- "path/to/your/file.gpkg"
layer <- st_read(gpkg_path, layer = "layer_name")

# Display information about the loaded layer


summary(layer)
plot(layer)

# Manipulate and save back to a GeoPackage


modified_layer <- layer # make desired modifications here
st_write(modified_layer, "path/to/new_file.gpkg", layer = "new_layer")

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)

# Connect to the GeoPackage as if it were a SQLite database


conn <- dbConnect(RSQLite::SQLite(), dbname = gpkg_path)

# List available tables (including spatial layers)


tables <- dbListTables(conn)
print(tables)

# Execute an SQL query on a non-spatial table or metadata


query <- dbGetQuery(conn, "SELECT * FROM gpkg_spatial_ref_sys LIMIT 10")
print(query)

# Close the connection


dbDisconnect(conn)

Integrating sf and RSQLite


While sf handles spatial data, RSQLite can be useful for querying attribute tables or
metadata within the GeoPackage. However, spatial operations like st_read() and
st_write() are exclusively managed by sf.

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)

# Load a spatial layer from a GeoPackage


gpkg_path <- "D:/mini/adress.gpkg"
layer <- st_read(gpkg_path, layer = "adress")

# Connect to the GeoPackage to query a non-spatial table


conn <- dbConnect(RSQLite::SQLite(), dbname = gpkg_path)
query <- dbGetQuery(conn, "SELECT * FROM adress")
dbDisconnect(conn)

# Manipulate the spatial layer in R (e.g., filter or transform)


filtered_layer <- layer[layer$z == "3", ]
8
# Save the new filtered layer in a new GeoPackage
st_write(filtered_layer, "D:/mini/adress_filtrado.gpkg", layer = "adress_filtrado")

1. GDAL/OGR with Integration into R (using system())


Example 1: Converting a GeoTIFF to Shapefile
In this example, we convert a raster file (GeoTIFF) to a vector format (Shapefile) using
the gdal_polygonize.py command via system() in R.

# Define input and output paths


raster_input <- "path_to_your_raster.tif"
shapefile_output <- "output_shapefile.shp"

# GDAL command to convert raster to shapefile


cmd <- paste("gdal_polygonize.py", raster_input, "-f 'ESRI Shapefile'", shapefile_output)
system(cmd)

Example 2: Merging Multiple Shapefiles into One


Here, we use the ogr2ogr command to merge multiple Shapefiles into a single file.

# List of Shapefiles to merge


shapefiles <- c("shapefile1.shp", "shapefile2.shp", "shapefile3.shp")
shapefile_output <- "output_merged.shp"

# GDAL command to merge Shapefiles


cmd <- paste("ogr2ogr -f 'ESRI Shapefile'", shapefile_output, shapefiles[1])
for (i in 2:length(shapefiles)) {
cmd <- paste(cmd, "&& ogr2ogr -f 'ESRI Shapefile' -update -append", shapefile_output,
shapefiles[i], "-nln", shapefile_output)
}
system(cmd)

Example 3: Simplifying Geometries of a Shapefile


This example demonstrates how to simplify geometries in a Shapefile using ogr2ogr to
reduce the number of vertices while maintaining the general shape.

# Path to input and output Shapefile


shapefile_input <- "path_to_your_shapefile.shp"
shapefile_output <- "output_simplified.shp"
tolerance <- 0.01 # Simplification tolerance

# GDAL command to simplify geometries


cmd <- paste("ogr2ogr -simplify", tolerance, shapefile_output,
shapefile_input)
system(cmd)

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.

2. GDAL/OGR in Command Line (cmd)


Example 1: Converting a Shapefile to KML
Convert a Shapefile to the KML format, widely used in applications like Google Earth.

ogr2ogr -f "KML" output.kml path_to_your_shapefile.shp

Example 2: Clipping a Raster with a Specific Extent


Here, we clip a raster to a specific area using a coordinate window.
gdal_translate -projwin xmin ymax xmax ymin path_to_your_raster.tif output_clip.tif

Example 3: Converting a Shapefile to GeoPackage


Convert a Shapefile to the GeoPackage format, which is a spatial database format.
ogr2ogr -f "GPKG" output.gpkg path_to_your_shapefile.shp

3. SQLite Using GeoPackage in R


Example 1: Creating and Populating a GeoPackage
We will create a new GeoPackage in R and add a spatial table using the sf package.

library(sf)

# Create an empty GeoPackage


gpkg_path <- "new_file.gpkg"
st_write(st_sf(geometry = st_sfc()), gpkg_path, layer = "empty_layer", driver = "GPKG")

# Add spatial data to the GeoPackage


data <- st_read("path_to_your_shapefile.shp")
st_write(data, gpkg_path, layer = "spatial_data", append = TRUE)

10
Example 2: Executing SQL Queries on a GeoPackage
Using RSQLite, we execute SQL queries directly on a GeoPackage.

library(RSQLite)
library(DBI)

# Connect to the GeoPackage


conn <- dbConnect(RSQLite::SQLite(), dbname = gpkg_path)

# Execute an SQL query to list the data


query <- dbGetQuery(conn, "SELECT * FROM spatial_data LIMIT 10")
print(query)

# Close the connection


dbDisconnect(conn)

Example 3: Manipulating Spatial Data in a GeoPackage


Here, we manipulate spatial data in R and save it in a GeoPackage using both sf and
RSQLite.

library(sf)
library(RSQLite)
library(DBI)

# Load the spatial layer


gpkg_path <- "path_to_your_gpkg.gpkg"
layer <- st_read(gpkg_path, layer = "spatial_data")

# Manipulate spatial data (e.g., filter by an attribute)


filtered_layer <- layer[layer$attribute == "value", ]

# Save the filtered layer back to the GeoPackage


st_write(filtered_layer, gpkg_path, layer = "filtered_data", append = TRUE)

# Check the results in the database


conn <- dbConnect(RSQLite::SQLite(), dbname = gpkg_path)
result <- dbGetQuery(conn, "SELECT * FROM filtered_data")
print(result)

# Close the connection


dbDisconnect(conn)

These examples demonstrate how to integrate GDAL/OGR with R using system(),


execute commands directly in the terminal, and how to manipulate geospatial data in R
using GeoPackage via SQLite. These practices are essential for efficient and automated
geoprocessing workflows in GIS environments.

11

You might also like