# Add point coordinates to a GeoTable

**URL:** <https://discourse.julialang.org/t/add-point-coordinates-to-a-geotable/125126>\
**Category:** Geo\
**Tags:** question, geo\
**Created:** [January 23, 2025, 2:27pm UTC](https://discourse.julialang.org/t/add-point-coordinates-to-a-geotable/125126 "2025-01-23T14:27:36Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![phchavesmaia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phchavesmaia/32/214892_2.png) [@phchavesmaia](https://discourse.julialang.org/u/phchavesmaia)\
**Post date:** [January 23, 2025, 2:27pm UTC](https://discourse.julialang.org/t/add-point-coordinates-to-a-geotable/125126/1 "2025-01-23T14:27:36Z")

</div>

Hello everyone,

I am trying to learn Julia and move some of my geostats projects to this language. I found that the GeoStats package is well regarded, hence I am using it. I have a grid and I want to separate the east from the west side of it. However, I am struggling to find how to retrieve x coordinates and append these values in the geotable. It follows a reproducible example:

```julia
using CSV, DataFrames, GeoStats

Aᵢ = CSV.read("a.csv", DataFrame; header = false) # 900x1 dataframe
grid = CartesianGrid(30, 30)
Aᵢ = georef(Aᵢ, grid)
f(x) = ustrip(coords(centroid(x)).x)<15 

Aᵢ = @transform(Aᵢ, :geometry => ByRow(f) => :Country) # ERROR: LoadError: ArgumentError: invalid expression syntax

```

Importantly, the f function is working as intended. This can be seen if I do

```julia
f.(Aᵢ.geometry)

```

Any help would be highly appreciated!

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [January 23, 2025, 2:54pm UTC](https://discourse.julialang.org/t/add-point-coordinates-to-a-geotable/125126/2 "2025-01-23T14:54:23Z")

</div>

Hi @phchavesmaia , welcome to the forum!

Try the `geosplit` utility:

```julia
  geosplit(object, fraction, [normal])

  Split geospatial object into two parts where the first part has a fraction of
  the elements. Optionally, the split is performed perpendicular to a normal
  direction.

```

I believe you want

```julia
geosplit(geotable, 0.5, (1, 0))

```

---

<div class="post-metadata">

**Author:** ![phchavesmaia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phchavesmaia/32/214892_2.png) [@phchavesmaia](https://discourse.julialang.org/u/phchavesmaia)\
**Post date:** [January 23, 2025, 3:12pm UTC](https://discourse.julialang.org/t/add-point-coordinates-to-a-geotable/125126/3 "2025-01-23T15:12:48Z")

</div>

Thank you very much, @juliohm , this indeed solved part of my problem!

Now, I just wanted to create a column (flag) in the main GeoTable indicating which cells belong to each section. What I am trying is:

```julia
using CSV, DataFrames, GeoStats, CairoMakie

data = CSV.read("a.csv", DataFrame; header = false)
rename!(data,[:Aᵢ])
data.cell = 1:size(data,1) 
grid = CartesianGrid(30, 30)
data = georef(data, grid)
data.Country = data.cell.∈ Ref(geosplit(data,0.5,(1,0))[1].cell) # ERROR: type GeoTable has no field Country

```

Any advice?

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [January 23, 2025, 3:36pm UTC](https://discourse.julialang.org/t/add-point-coordinates-to-a-geotable/125126/4 "2025-01-23T15:36:08Z")

</div>

It feels like an instance of the [XY problem](https://en.wikipedia.org/wiki/XY_problem). Could you please describe what are trying to achieve as your final objective? Did you read the GDSJL book?

[https://juliaearth.github.io/geospatial-data-science-with-julia](https://juliaearth.github.io/geospatial-data-science-with-julia)

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [January 23, 2025, 5:18pm UTC](https://discourse.julialang.org/t/add-point-coordinates-to-a-geotable/125126/5 "2025-01-23T17:18:25Z")

</div>

One possible way to create a new column based on the x coordinate:

```julia
@transform(geotable, :column = coords(:geometry).x < 100km)

```

---

<div class="post-metadata">

**Author:** ![phchavesmaia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phchavesmaia/32/214892_2.png) [@phchavesmaia](https://discourse.julialang.org/u/phchavesmaia)\
**Post date:** [January 23, 2025, 5:39pm UTC](https://discourse.julialang.org/t/add-point-coordinates-to-a-geotable/125126/6 "2025-01-23T17:39:45Z")

</div>

My final objective is to replicate Redding and Rossi-Hansberg (2017) in julia, while the original code is in Matlab. I know that @SebKrantz has already dabbled into this endeavor, but I want to take my own shot at it.

In this paper, the authors generate a 30 by 30 grid representing two bordering countries that trade with each other while facing export tarifs. I am trying to generate the such grid, indicating to which country a cell belongs, in order to reproduce their findinds.

That said, differently from them, I would like to reproduce the model while using georeferenced data, which is the kind of data I would actually use in a real research scenario.

---

<div class="post-metadata">

**Author:** ![phchavesmaia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phchavesmaia/32/214892_2.png) [@phchavesmaia](https://discourse.julialang.org/u/phchavesmaia)\
**Post date:** [January 23, 2025, 5:42pm UTC](https://discourse.julialang.org/t/add-point-coordinates-to-a-geotable/125126/7 "2025-01-23T17:42:34Z")

</div>

I tried to do it, but it returns the following error: ArgumentError: column name “x” not found in the data frame; existing most similar names are: “Aᵢ”

What I did was to create an auxiliary table to store the values (as in the GDSJL book). I made all the operations regarding values using this auxiliary table and then just plugged the geometries back. Somewhat convoluted, but it worked!

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [January 23, 2025, 5:53pm UTC](https://discourse.julialang.org/t/add-point-coordinates-to-a-geotable/125126/8 "2025-01-23T17:53:46Z")

</div>

I believe we encountered a bug in which the expression `coords(:geometry).x` is not correctly understood by the `@transform` macro. Will take a look, thanks for reporting.

Please let us know if something else is not working as expected, or if you need additional help.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [January 23, 2025, 6:28pm UTC](https://discourse.julialang.org/t/add-point-coordinates-to-a-geotable/125126/9 "2025-01-23T18:28:32Z")

</div>

Here is a valid solution:

```julia
fun(point) = coords(point).x < 10km

@transform(geotable, :column = fun(:geometry))

```

Try to define the function outside the macro.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [January 27, 2025, 10:46am UTC](https://discourse.julialang.org/t/add-point-coordinates-to-a-geotable/125126/10 "2025-01-27T10:46:18Z")

</div>

@phchavesmaia we identified the root of the problem: the expression `obj.field` is converted to `obj.:field` by Julia and the resulting symbol `:field` is incorrectly interpreted as a column in the `@transform` macro.

We will treat this as a special case like other packages do (e.g., DataFramesMeta.jl). You can watch the corresponding issue to get notifications when it is fixed:

> <https://github.com/JuliaEarth/GeoStats.jl/issues/506>
>
> Currently, the following code doesn't work:
> 
> \`\`\`julia
> @transform(geotable, :colu…mn = coords(:geometry).x \< 100km)
> \`\`\`
> 
> The workaround consists of defining the function outside the macro:
> 
> \`\`\`julia
> fun(point) = coords(point).x \< 100km
> 
> @transform(geotable, :column = fun(:geometry))
> \`\`\`
> 
> We need to review the macro to handle arbitrary expressions.

---

<div class="post-metadata">

**Author:** ![phchavesmaia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phchavesmaia/32/214892_2.png) [@phchavesmaia](https://discourse.julialang.org/u/phchavesmaia)\
**Post date:** [January 27, 2025, 6:24pm UTC](https://discourse.julialang.org/t/add-point-coordinates-to-a-geotable/125126/11 "2025-01-27T18:24:08Z")

</div>

This worked perfectly, thank you!

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [February 7, 2025, 12:25pm UTC](https://discourse.julialang.org/t/add-point-coordinates-to-a-geotable/125126/12 "2025-02-07T12:25:34Z")

</div>

@phchavesmaia the fix is awaiting review:

> <https://github.com/JuliaEarth/GeoTables.jl/pull/140>
>
> Fix https://github.com/JuliaEarth/GeoStats.jl/issues/506

I will post here when the patch release is triggered.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [February 7, 2025, 3:05pm UTC](https://discourse.julialang.org/t/add-point-coordinates-to-a-geotable/125126/13 "2025-02-07T15:05:51Z")

</div>

We released a patch with the fix. Should be available in a few minutes:

> <https://github.com/JuliaRegistries/General/pull/124542>
>
> \- Registering package: GeoTables
> \- Repository: https://github.com/JuliaEarth/Geo…Tables.jl
> \- Created by: @juliohm
> \- Version: v1.24.10
> \- Commit: c8940c12808582cfac590df22f35cd7b958ee4bb
> \- Reviewed by: @juliohm
> \- Reference: https://github.com/JuliaEarth/GeoTables.jl/commit/c8940c12808582cfac590df22f35cd7b958ee4bb#commitcomment-152276859
> \- Description: Geospatial tables compatible with the GeoStats.jl framework
