Read and use .gpkg file

Hi,
Hereโ€™s my latest challenge:

Iโ€™m looking to get the 5km tiles out of this file: os_bng_grids.7z.

I extracted the content, a 200MB gpgk file of the same name as the 7z file, and can load this effortlessly using GeoIO.jl but I cannot coax it to reveal its structure or content in a way that I can access - or at all infact. :worried:

    geo = GeoIO.load("os_bng_grids.gpkg")
    println(values(geo))
    println((geo[geo.tile_name.=="HL", ["geometry"]]))

shows the tile_names, but these are the names of the 100km squares. Iโ€™m looking for the names and grid coords of the 5km squares.

(tile_name = ["HL", "HM", "HN", "HO", "HP", "HQ", "HR", "HS", "HT", "HU", "HV", "HW", "HX", "HY", "HZ", "JL", "JM", "JQ", "JR", "JV", "JW", "NA", "NB", "NC", "ND", "NE", "NF", "NG", "NH", "NJ", "NK", "NL", "NM", "NN", "NO", "NP", "NQ", "NR", "NS", "NT", "NU", "NV", "NW", "NX", "NY", "NZ", "OA", "OB", "OF", "OG", "OL", "OM", "OQ", "OR", "OV", "OW", "SA", "SB", "SC", "SD", "SE", "SF", "SG", "SH", "SJ", "SK", "SL", "SM", "SN", "SO", "SP", "SQ", "SR", "SS", "ST", "SU", "SV", "SW", "SX", "SY", "SZ", "TA", "TB", "TF", "TG", "TL", "TM", "TQ", "TR", "TV", "TW"],)
1ร—1 GeoTable over 1 view(::GeometrySet, [1])

This must be simple, but itโ€™s currently defeating me!

Can anyone point me in the right direction?

Thanks!

Hi @TimG , can you please explain in more detail what you are trying to achieve? Please tag your questions with geo or submit it to the Geo community here on Discourse. We donโ€™t get notifications otherwise.

I think I got you. What you need is to specify the layer of the file that you want to load. The package loads layer=0 by default. Try the following:

GeoIO.load("os_bng_grids.gpkg", layer=1)

There are 6 layers in this file (0, 1, โ€ฆ, 5).

1 Like

I have a largish dataset of UK addresses in a dataframe. Each address has a grid reference locating it to the nearest metre on the British National Grid. I need to tag each address with the name of the 5km grid tile it lies in.

If/when Iโ€™ve read and understood the grid data, my plan of attack was

  1. Convert the grid reference to a geometry, like:
points = Point.(zip(df.EASTING, df.NORTHING))
newdf = georef(df, points)
  1. geojoin the address points to the grid tile geometry:
joineddf = geojoin(newdf, tilegeometry, kind=:left, pred=((g1, g2) -> intersects(g1, g2)))

This may be a bit of a sledge hammer to crack a nut, though. The grid tiles are systematically laid out. The key bit I donโ€™t know is the two letter codes used for the 100km tiles (which are also the first two characters of the 5km tile names). Once the two letters are known, the digits are easily computable from the full grid reference, I think.

So I am now thinking that I just need to find the co-ordinates of the south-west corner of each 100km grid tile and the associated two-letter code for that tile. From that I can create a look-up from any grid location to the relevant 5km grid tile. I only need to do this once and then using the look-up ought to be faster than using geojoin (although geojoin seems quite fast and my code is generally very inefficient!)

1 Like

Thank you. Yes, the layers are the key that I didnโ€™t know. Layer 4 contains the 5km grid and has the following column names:
["tile_name", "1km_grid_ref", "geometry"]

The documentation for this dataset says

The 5km grid also contains '1km_grid_ref' which relates to the 1km grid cell
at the South-West corner of each cell
 e.g. 'tile_name' = "SP19SW" and 'tile_grid_ref' = "SP1090"

I actually need the name of the tile_grid_ref, which is made up of the two-letter code for the 100km tile it lies within and the 4-digit grid reference of the south-west corner of the relevant 5km tile within it.

In what is presumably a bad habit, I donโ€™t develop my code in the REPL, but use VSCode, and just run without debugging as I go along.

Doing this to load the gpkg file:

geo = GeoIO.load("os_bng_grids.gpkg", layer=4)
println(geo)
println((geo[1:10, ["geometry"]]))

generates the relatively uninformative message:

36400ร—3 GeoTable over 36400 GeometrySet
10ร—1 GeoTable over 10 view(::GeometrySet, 1:10)

which is a little too much of a black box for me!

If I do the same in the REPL, I get

julia> geo = GeoIO.load("os_bng_grids.gpkg", layer=4)
36400ร—3 GeoTable over 36400 GeometrySet
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  tile_name  โ”‚ 1km_grid_ref โ”‚                                geometry                                โ”‚
โ”‚ Categorical โ”‚ Categorical  โ”‚                                PolyArea                                โ”‚
โ”‚  [NoUnits]  โ”‚  [NoUnits]   โ”‚                  ๐Ÿ–ˆ ShiftedTransverseMercator{OSGB36}                    โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚   HL00NE    โ”‚    HL0505    โ”‚ PolyArea((x: 5000.0 m, y: 1.205e6 m), ..., (x: 5000.0 m, y: 1.21e6 m)) โ”‚
โ”‚   HL00NW    โ”‚    HL0005    โ”‚    PolyArea((x: 0.0 m, y: 1.205e6 m), ..., (x: 0.0 m, y: 1.21e6 m))    โ”‚
โ”‚   HL00SE    โ”‚    HL0500    โ”‚ PolyArea((x: 5000.0 m, y: 1.2e6 m), ..., (x: 5000.0 m, y: 1.205e6 m))  โ”‚
โ”‚   HL00SW    โ”‚    HL0000    โ”‚    PolyArea((x: 0.0 m, y: 1.2e6 m), ..., (x: 0.0 m, y: 1.205e6 m))     โ”‚
โ”‚   HL01NE    โ”‚    HL0515    โ”‚ PolyArea((x: 5000.0 m, y: 1.215e6 m), ..., (x: 5000.0 m, y: 1.22e6 m)) โ”‚
โ”‚   HL01NW    โ”‚    HL0015    โ”‚    PolyArea((x: 0.0 m, y: 1.215e6 m), ..., (x: 0.0 m, y: 1.22e6 m))    โ”‚
โ”‚   HL01SE    โ”‚    HL0510    โ”‚ PolyArea((x: 5000.0 m, y: 1.21e6 m), ..., (x: 5000.0 m, y: 1.215e6 m)) โ”‚
โ”‚   HL01SW    โ”‚    HL0010    โ”‚    PolyArea((x: 0.0 m, y: 1.21e6 m), ..., (x: 0.0 m, y: 1.215e6 m))    โ”‚
โ”‚   HL02NE    โ”‚    HL0525    โ”‚ PolyArea((x: 5000.0 m, y: 1.225e6 m), ..., (x: 5000.0 m, y: 1.23e6 m)) โ”‚
โ”‚   HL02NW    โ”‚    HL0025    โ”‚    PolyArea((x: 0.0 m, y: 1.225e6 m), ..., (x: 0.0 m, y: 1.23e6 m))    โ”‚
โ”‚   HL02SE    โ”‚    HL0520    โ”‚ PolyArea((x: 5000.0 m, y: 1.22e6 m), ..., (x: 5000.0 m, y: 1.225e6 m)) โ”‚
โ”‚   HL02SW    โ”‚    HL0020    โ”‚    PolyArea((x: 0.0 m, y: 1.22e6 m), ..., (x: 0.0 m, y: 1.225e6 m))    โ”‚
โ”‚   HL03NE    โ”‚    HL0535    โ”‚ PolyArea((x: 5000.0 m, y: 1.235e6 m), ..., (x: 5000.0 m, y: 1.24e6 m)) โ”‚
โ”‚   HL03NW    โ”‚    HL0035    โ”‚    PolyArea((x: 0.0 m, y: 1.235e6 m), ..., (x: 0.0 m, y: 1.24e6 m))    โ”‚
โ”‚   HL03SE    โ”‚    HL0530    โ”‚ PolyArea((x: 5000.0 m, y: 1.23e6 m), ..., (x: 5000.0 m, y: 1.235e6 m)) โ”‚
โ”‚   HL03SW    โ”‚    HL0030    โ”‚    PolyArea((x: 0.0 m, y: 1.23e6 m), ..., (x: 0.0 m, y: 1.235e6 m))    โ”‚
โ”‚   HL04NE    โ”‚    HL0545    โ”‚ PolyArea((x: 5000.0 m, y: 1.245e6 m), ..., (x: 5000.0 m, y: 1.25e6 m)) โ”‚
โ”‚   HL04NW    โ”‚    HL0045    โ”‚    PolyArea((x: 0.0 m, y: 1.245e6 m), ..., (x: 0.0 m, y: 1.25e6 m))    โ”‚
โ”‚   HL04SE    โ”‚    HL0540    โ”‚ PolyArea((x: 5000.0 m, y: 1.24e6 m), ..., (x: 5000.0 m, y: 1.245e6 m)) โ”‚
โ”‚   HL04SW    โ”‚    HL0040    โ”‚    PolyArea((x: 0.0 m, y: 1.24e6 m), ..., (x: 0.0 m, y: 1.245e6 m))    โ”‚
โ”‚   HL05NE    โ”‚    HL0555    โ”‚ PolyArea((x: 5000.0 m, y: 1.255e6 m), ..., (x: 5000.0 m, y: 1.26e6 m)) โ”‚
โ”‚      โ‹ฎ      โ”‚      โ‹ฎ       โ”‚                                   โ‹ฎ                                    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                                                                     36379 rows omitted

julia> 

which is much more informative! (Wouldnโ€™t have told me about the layers, but does open the black-box dataset a bit!).

1 Like

Are you aware of the VSCode shortcuts to run lines of the script in the REPL? For example, press Ctrl+Enter to send the line to the REPL. You can press Ctrl+Shift+p and type โ€œJuliaโ€ to learn about all commands related to Julia.

You are gradually acquiring new geospatial skills, and that is really nice! :tada:

These functions will get faster over time. There are various performance optimizations in our TODO list related to geojoin.

1 Like