# Grouping geommetries by colums

**URL:** <https://discourse.julialang.org/t/grouping-geommetries-by-colums/122241>\
**Category:** General Usage\
**Tags:** package, plot, graphs, archgdal\
**Created:** [November 4, 2024, 5:34pm UTC](https://discourse.julialang.org/t/grouping-geommetries-by-colums/122241 "2024-11-04T17:34:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daves\_Cordova](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daves_cordova/32/210931_2.png) [@Daves\_Cordova](https://discourse.julialang.org/u/Daves_Cordova)\
**Post date:** [November 4, 2024, 5:34pm UTC](https://discourse.julialang.org/t/grouping-geommetries-by-colums/122241/1 "2024-11-04T17:34:13Z")

</div>

I need to combine geometries for a shapefile. Initially, there is shapefile\_path\_cities that will form the layer behind, after theres is shapefile\_path\_highways:

```julia
using GeoDataFrames
using DataFrames
using XLSX
using Plots
using Shapefile
using Random
using Statistics
using Base

###Shapefile municipalities

shapefile_path = "C:\\Users\\daves\\OneDrive\\Pessoal\\Acadêmico\\Mestrado\\Dissertação - Execução\\Análises\\MT_Municipios_2022\\MT_Municipios_2022.shp"
municipalities = Shapefile.Table(shapefile_path) |> DataFrame

# Highway shapefile
shapefile_path_highways = "C:\\Users\\daves\\OneDrive\\Pessoal\\Acadêmico\\Mestrado\\Dissertação - Execução\\Análises\\Rodovias\\SNV_202410A.shp"
highways = Shapefile.Table(shapefile_path_rodovias) |> DataFrame

```

These data on shapefile\_path\_highway, which be puted in the dataframe highways will be filtered and grouped:

```julia
# Filtering sg_uf equals to MT
filtered_highwayss_MT = filter(row -> row.sg_uf == "MT", rodovias)

# Grouping by vl_br row
grouped_highways = groupby(filtered_highways_MT, :vl_br)

```

Untill now everthing done succesfully, however the combine function did not work well:

```julia
#Combine geometries
function combine_geometries(geometries)
    combined_geom = ArchGDAL.readgeometry(geometries[1].geometry)
    for geom in geometries[2:end]
        combined_geom = ArchGDAL.union!(combined_geom, ArchGDAL.readgeometry(geom.geometry))
    end
    return combined_geom
end

# Applying function
combined_geometries = combine(grouped_rodovias, :geometry => combine_geometries => :geometry)

#Showing results
println(combined_geometries)

```

If you want to take shapefiles the first is in and [https://geoftp.ibge.gov.br/organizacao\_do\_territorio/malhas\_territoriais/malhas\_municipais/municipio\_2022/UFs/MT/MT\_Municipios\_2022.zip](https://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_municipais/municipio_2022/UFs/MT/MT_Municipios_2022.zip) the second is in [https://servicos.dnit.gov.br/dnitcloud/index.php/s/oTpPRmYs5AAdiNr/download](https://servicos.dnit.gov.br/dnitcloud/index.php/s/oTpPRmYs5AAdiNr/download). The first is provided by IBGE and the second by DNIT, both agencies of Brazilian government.

There is an argument to combine geometries that can function instead of readgeometry and union!?

---

<div class="post-metadata">

**Author:** ![asinghvi17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asinghvi17/32/8272_2.png) [@asinghvi17](https://discourse.julialang.org/u/asinghvi17)\
**Post date:** [November 5, 2024, 5:44am UTC](https://discourse.julialang.org/t/grouping-geommetries-by-colums/122241/2 "2024-11-05T05:44:30Z")

</div>

> [@Daves\_Cordova](#):
>
> ```julia
> function combine_geometries(geometries)
> combined_geom = ArchGDAL.readgeometry(geometries[1].geometry)
> for geom in geometries[2:end]
> combined_geom = ArchGDAL.union!(combined_geom, ArchGDAL.readgeometry(geom.geometry))
> end
> return combined_geom
> end
> 
> ```

Shapefile.jl geometries are GeoInterface compatible, so you should use `GeoInterface.convert(ArchGDAL, geometry)` to convert them. You will likely get an error message trying to use ArchGDAL internal functions on Shapefile.jl geometries (also I can’t find any function named `readgeometry` in ArchGDAL? `union!` is also not defined for ArchGDAL geometries…)

Try something like this:

```julia
function combine_geometries(geometries)

    return mapfoldl(
        x -> GeoInterface.convert(ArchGDAL, x), # map - apply to each element
        ArchGDAL.union, # reduce 2 elements to 1 element
        geometries # the vector to perform the operation on
    )
end

```

---

<div class="post-metadata">

**Author:** ![Daves\_Cordova](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daves_cordova/32/210931_2.png) [@Daves\_Cordova](https://discourse.julialang.org/u/Daves_Cordova)\
**Post date:** [November 5, 2024, 2:33pm UTC](https://discourse.julialang.org/t/grouping-geommetries-by-colums/122241/3 "2024-11-05T14:33:28Z")

</div>

Thanks, this code worked well!
