0

[TLDR] I am working on converting a segmentation model output into a shapefile that can be added as a layer on a map. I was hoping to find how to make each pixel value have a different color instead of all black?

I am relatively new to all of this. So far, I've been able to generate the shapefile using the script below:

from osgeo import gdal, ogr, osr
raster = gdal.Open(r'test.tif')
band = raster.GetRasterBand(1)
array = band.ReadAsArray()

proj = raster.GetProjection()
shp_proj = osr.SpatialReference()
shp_proj.ImportFromWkt(proj)

output_file = 'test.shp'
call_drive = ogr.GetDriverByName('ESRI Shapefile')
create_shp = call_drive.CreateDataSource(output_file)

shp_layer = create_shp.CreateLayer('LandCover', srs = shp_proj)
new_field = ogr.FieldDefn(str('ID'), ogr.OFTInteger)
shp_layer.CreateField(new_field)

gdal.Polygonize(band, None, shp_layer, 0, [], callback=None)
create_shp.Destroy()
raster = None

And it looks like this on QGIS: enter image description here

What the original TIFF looks like enter image description here

Are there any ways we can generate the shapefile so that each pixel values have different colors? eg

  • 0 => transparent
  • 1 => blue
  • 2 => green etc.
5
  • A shapefile doesn't come with colors. You need to style it, for example in QGIS: qgistutorials.com/en/docs/basic_vector_styling.html . The style is saved in the map document.
    – Bera
    Commented Oct 7, 2022 at 11:44
  • I was able to do this in QGIS. But I'm exploring a way to do it in a script.
    – Ike Nna
    Commented Oct 7, 2022 at 12:28
  • 1
    You cant. The style is not saved in the shapefile. It depends on which software you are using to display it. For example in QGIS you can save the style as a qml file, in ArcGIS a lyr
    – Bera
    Commented Oct 7, 2022 at 13:18
  • There are some software specific ways to store style into an attribute field. What might work with QGIS is to add an attribute that is named OGR_STYLE into the shapefile and save the style string as defined in gdal.org/user/ogr_feature_style.html. Or you can have a look at other formats that have more or less featured style support like KML gdal.org/drivers/vector/libkml.html or OpenJUMP JML gdal.org/drivers/vector/jml.html.
    – user30184
    Commented Oct 7, 2022 at 13:58
  • Thank you all for your clarifications! I decided to stick to exporting the style after formating on ArcGIS.
    – Ike Nna
    Commented Oct 11, 2022 at 5:50

1 Answer 1

0

I decided to stick to exporting the style after formatting on QGIS. That is:

  1. Importing the Shapefile created using the python script in the question to QGIS
  2. Styling the Shapefile via the Properties->Symbology section.
  3. Export the created layer
1
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jun 7, 2023 at 7:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.