# Convert binary mask to shapefile

**URL:** <https://discourse.julialang.org/t/convert-binary-mask-to-shapefile/97322>\
**Category:** Geo\
**Created:** [April 10, 2023, 10:19pm UTC](https://discourse.julialang.org/t/convert-binary-mask-to-shapefile/97322 "2023-04-10T22:19:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chadagreene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chadagreene/32/34924_2.png) [@chadagreene](https://discourse.julialang.org/u/chadagreene)\
**Post date:** [April 10, 2023, 10:19pm UTC](https://discourse.julialang.org/t/convert-binary-mask-to-shapefile/97322/1 "2023-04-10T22:19:59Z")

</div>

I have a large (20,000 x 25,000) binary mask that defines the shape of an island.

What’s the best way to convert the mask into a polygon and and save it as a shapefile?

---

<div class="post-metadata">

**Author:** ![sidpku](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sidpku/32/45729_2.png) [@sidpku](https://discourse.julialang.org/u/sidpku)\
**Post date:** [April 11, 2023, 1:39am UTC](https://discourse.julialang.org/t/convert-binary-mask-to-shapefile/97322/2 "2023-04-11T01:39:17Z")

</div>

I think Qgis is convenient to do so.

here is a manual about how to convert raster to polygon.shp

[https://docs.qgis.org/](https://docs.qgis.org/3.28/en/docs/training_manual/complete_analysis/raster_to_vector.html)

---

<div class="post-metadata">

**Author:** ![chadagreene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chadagreene/32/34924_2.png) [@chadagreene](https://discourse.julialang.org/u/chadagreene)\
**Post date:** [April 11, 2023, 3:43pm UTC](https://discourse.julialang.org/t/convert-binary-mask-to-shapefile/97322/3 "2023-04-11T15:43:11Z")

</div>

Thanks, @sidpku. So no way to do it in Julia? I’ve got about 600 masks to convert to shapefiles, currently in the form of a 20000x25000x600 data cube in a NetCDF. I was hoping to write a Julia workflow that could do this automatically. Dreaming of a function like

```julia
mask2shapefile(x, y, mask, filename="mynewfile.shp", epsg_code=1234)

```

---

<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:** [April 11, 2023, 3:51pm UTC](https://discourse.julialang.org/t/convert-binary-mask-to-shapefile/97322/4 "2023-04-11T15:51:26Z")

</div>

@chadagreene we didn’t try our algorithms with such large images yet, but we have a transform called `Potrace` (polygon tracing) in the GeoStats.jl stack:

```julia
using GeoStats

# georeference mask image
dat = georef((mask=mask,))

# trace shape from mask variable
shape = dat |> Potrace(:mask)

```

Please let us know if you find performance bottlenecks.

This algorithm was quite tricky to implement correctly. It handles any arbitrary number of nested holes in the shape.

---

<div class="post-metadata">

**Author:** ![chadagreene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chadagreene/32/34924_2.png) [@chadagreene](https://discourse.julialang.org/u/chadagreene)\
**Post date:** [April 11, 2023, 3:57pm UTC](https://discourse.julialang.org/t/convert-binary-mask-to-shapefile/97322/5 "2023-04-11T15:57:11Z")

</div>

ok, I’ll give it a shot!
