Skip to main content

Julia programming: My Journey Begins at EGU

The Beginning

It's been a long time since I first heard about Julia. Despite my interest, I never got around to actually studying it. That changed recently when I attended a short course on Julia at the last EGU General Assembly. Inspired by what I learned, I returned home determined to dive into programming with Julia.

First Steps

I started with a basic book on Julia, aiming to build a solid foundation. The simplicity and power of the language fascinated me, and soon, I felt ready to take a more hands-on approach.

My Project: A Julia Package

As I gained confidence, I began writing my own package. This project is still a work in progress on github. The package includes:

  • Reading Time Series: It can read a time series from a CSV file, handling special formatting and metadata.
  • Goodness of Fit Functions.
  • Graph Operations on river networks.(in progres)
  • Geometry/Geography Operations.

Focus on Geography

In this blog post, I want to delve into the last point — geometry and geography operations. My initial goal was simple: create a grid of points within a specified region. This function lays the groundwork for more complex spatial analyses.

Essential Libraries for Geospatial Processing in Julia

  • ArchGDAL: This library is crucial for reading and manipulating raster and vector data, forming the backbone of our data handling.
  • GeoDataFrames: Built upon ArchGDAL, GeoDataFrames is perfect for reading shapefiles, allowing for easy integration and manipulation of vector data. It also allows the reprojection of features. In this case, pay attention to use the order argument properly, as you can see in this thread.
  • GeoFormatTypes: This library manages specific geospatial data types, such as coordinate reference systems (CRS). It's a utility library.
  • LibGEOS: For performing complex spatial operations, LibGEOS provides a robust set of tools that are indispensable for any geospatial analysis, particularly when constructing and manipulating geometries, such as unions, intersections, overlaps, etc.

Here is an example of how to reproject a DataFrame with geometry columns (obtained by reading a shapefile with GeoDataFrames) using ArchGDAL and GeoDataFrames, plus GeoFormatTypes:

using GEOFRAMEjl
import ArchGDAL as GDAL
import GeoDataFrames as GDF
import GeoFormatTypes as GFT
using DataFrames

# read the vector file and create a DataFrame with a geometry column
basins_file = ArchGDAL.read("./basin_km3.shp")
basins_layer = ArchGDAL.getlayer(basins_file)
source_crs = ArchGDAL.getspatialref(basin_layer)
target_crs = ArchGDAL.importEPSG(32632, order=:trad)

ArchGDAL.createcoordtrans(source_crs, target_crs) do transform
    for i in 1:nrow(basins)
        geom = basins[i, 1]
        ArchGDAL.transform!(geom, transform)
    end
end

# with GeoDataFrames
basins = GDF.read("./basin_km3.shp")

function get_epsg(df)
    a = GDF.metadata(df)["crs"]
    return convert(GFT.EPSG, a).val[1]
end

source_crs = GFT.EPSG(get_epsg(basins))
target_crs = GFT.EPSG(32632)

basins.geometry = GDF.reproject(basins.geometry, source_crs, target_crs, order=:trad)

Creating a Grid with Julia

Now, let's dive into how I utilize these tools to create a grid of points within a specified region:

using GEOFRAMEjl
import GeoDataFrames as GDF
import ArchGDAL as GDAL
using GeoArrays

# create a squared grid
step = 500
full_grid = Geo.create_grid(614000, 680500, step, 5112000, 5159000, step)

# read polygons (3km2)
basins = GDF.read("./basin_km3.shp")
grid = Geo.filter_points(full_grid, basins, buffer=b)
dem = GeoArrays.read("./dtm_tot_5.tif", band=1)
grid = Geo.get_value_from_raster(grid, dem)
Geo.save_grid(grid, "./grid.shp", 32632)

This generates:

For reprojection, pay attention to the order; more details are available in the thread linked above.

Comments

Popular posts from this blog

Read magnetic strip card data in Android

Recently I have developed an app with the purpose to read data from an USB card reader. The data to be read are stored on a plastic card organized in this way:  a start tag; a fixed field (length of 17 char); two other fields separated by a white space with no size declarad but always present; an end tag.  The external USB card reader is something similar to an external keyboard so it's possible to override the appropriate method in the  Activity to handle every single character read from the reader. The Activity manages the card data character by character so  firstly I had to create a UsbDecoder class to manage the data flow and to build my output values. Lastly. I had to implemented the onKeyDown to intercept the data from the reader: @Override public boolean onKeyDown(int keyCode, KeyEvent event) { char pressedKey; if (event.getKeyCode() == KeyEvent.KEYCODE_SPACE || event.getKeyCode() == KeyEvent.KEYCODE_TAB) { pressedKey = Character

Efficient Android Threading, By Anders Goransson, O'Reilly Media

I'm not a Java threading expert , so I decided to spend some of my spare time to study it . I started from scratch and then I move to threading in the Android Platform. To achieve this goal I used the Efficient Android Threading book , written by Anders Goransson. Obviously the guide isn't for beginners of Android programming and it requires a minimal knowledge of Android SDK. First chapters are dedicated to the basics of threading and how the operating system manages the multi-threading. These chapters allowed me to memorize  some concept and to have a better background about the system threading management. The following chapters are focused on how to use the Android framework's multi-threading tools . In my opinion this book offers to the readers the opportunity to become familiar with the whole range of techniques that allow to build more responsive Android Apps by using threads. I have appreciated the comparison between available techniques listed in the

On the smartphone GPS accuracy

 As i started playing around Android programming, as soon I got the grip on the basic, I had go deep into "localization". So I had read a lot of manual pages, web pages and post about it. My first step was to create a class which allow me to select the best location provider, in several study case. So my mainly choice parameter were the requested accuracy, the  status of the battery, and if I get the passive data then the age of the Location Object. Two fact drawing my attention: the passive provider , and the improve of precision of coordinates when the wi-fi was turned on. So I have investigate how it was work, and I found several blog article or post [2], which explain how it work, and support it by example on how happen when people relocate  without changes ("relocate") theirs  their's wi-fi hot spot: a catastrophic lack in accuracy of the Location. However in the last days I have had some spare time so I have tried to better understand the accurac