python: how to generate a json file with polygons of the same color but different opacities

1k Views Asked by At

I am using python to generate a json file which contains a lot of polygons and a value is assigned to each polygon, which looks like:

[{'cell':{'type':'Polygon', 'coordinates':[[[121,47],[122,48],[122,47],[121,47]]]}, 'value':2.45},
 {'cell':{'type':'Polygon', 'coordinates':[[[120,48],[123,48.5],[122,48],[120,48]]]}, 'value':1.45},
 ...]

I want to plot these polygons on geojson.io and every cell is of color red. But the opacity of each polygon is dependent on the value, the higher value, the more opaque; the smaller value, the less opaque.

I think I should do normalization of all the values at first, and then I need to use the

Feature(properties={'fill':'#ff0000', 'opacity':normalized_value})

But I think this is a little bit complicated. Can I just use the original values for each cell in the properties, and the opacity will be automatically adjusted?

1

There are 1 best solutions below

0
On

So I went onto geojson.io. And I got:

{
    "type": "Feature",
    "properties": {
        "stroke": "#555555",
        "stroke-width": 2,
        "stroke-opacity": 1,
        "fill-opacity": 0.67,
        "fill": "#ff0000"
    },
    "geometry": {
        "type": "Polygon",
        "coordinates": []
    }
}

I'm ignoring the coordinates at the moment, as they take up too much space. First lets make a small class, that returns a dict. with the settings it must have, the colour and the opacity.

class Polygon:

    def __init__(self, opacity, coordinates):
        self.opacity = opacity
        self.fill = "#ff0000"

        c=coordinates
        coordinates = [[
            c[0],
            c[1],
            c[2],
            c[3],
            c[0]
        ]]
        self.coordinates = coordinates

    def json(self):
        return {
            "type": "Feature",
            "properties": {
                "fill-opacity": self.opacity,
                "fill": self.fill
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": self.coordinates
            }
        }

features = [Polygon(random.random(), rand_coords()) for i in range(10)]
print(json.dumps({
    "type": "FeatureCollection",
    "features": [i.json() for i in features]
}))

If you want to be able to use the cell format you have. You can do this.

cell_list = [
    {'cell':{'type':'Polygon', 
    'coordinates':[[[121,47],[122,48],[122,47],[121,47]]]}, 'value':2.45},
    {'cell':{'type':'Polygon',
    'coordinates':[[[120,48],[123,48.5],[122,48],[120,48]]]}, 'value':1.45}
]

#get a list of polygons.
features = [
    Polygon(cell['value'], cell['cell']['coordinates'][0]) for cell in cell_list
]

#largest value
max_opacity = max(feature.opacity for feature in features)

#normalise
for feature in features:
    feature.opacity /= max_opacity

#get the compatible json data
 print(json.dumps({
    "type": "FeatureCollection",
    "features": [i.json() for i in features]
}, sort_keys=True, indent=4, separators=(',', ': ')))

This is not the best working system. When I executed it, and copied the output I got triangles that were in the top-right of china... However the triangles opacity were different.

I didn't really do anything with the shape, as that was not asked. However I made it possible to change the input to valid output. All red, with relative opacity.

You can check if it looks alright here. However hover over the shapes to see what there settings are. Mine come up blue, with the colour settings '#ff0000'.