Hi @anjulia
You can draw polygons in lon- lat coordinates, defined asmapbox layers.
Here is an example of recatngular shape over Sweden:
import plotly.graph_objs as go
mapbox_t=open(".mapbox_token").read().rstrip()
data = [go.Scattermapbox(
lat=[62.3833], # [57.671667] ,
lon=[16.3000], # [11.980833],
mode='markers',
marker=dict(
size= 3,
color = 'red',
opacity = .8,
))]
#set the layout to plot
layout = go.Layout(autosize=True,
mapbox = dict(center= dict(lat=62.3833, #Set center at Flataklocken
lon=16.3000),
accesstoken=mapbox_t,
zoom=4,
style='light',
),
width=700,
height=600,
title = 'Sweden')
fig = go.Figure(data=data, layout=layout)
The following function defines a geojson-type dict corresponding to each polygon, no matter how many vertices it has:
def get_polygon(lons, lats, color='blue'):
if len(lons) != len(lats):
raise ValueError('the legth of longitude list must coincide with that of latitude')
geojd = {"type": "FeatureCollection"}
geojd['features'] = []
coords = []
for lon, lat in zip(lons, lats):
coords.append((lon, lat))
coords.append((lons[0], lats[0])) #close the polygon
geojd['features'].append({ "type": "Feature",
"geometry": {"type": "Polygon",
"coordinates": [coords] }})
layer=dict(sourcetype = 'geojson',
source =geojd,
below='',
type = 'fill',
color = color)
return layer
Now let us define a simple polygon:
mylayers =[]
mylayers.append(get_polygon(lons=[14, 16, 16, 14], lats=[58.55, 58.55, 60.6, 60.6], color='gold'))
fig.layout.update(mapbox_layers =mylayers);
Unfortunately we cannot plot polygons only inside th Goteborg boundaries, because they have very small lengths (less than 1 degree), and plotly cannot draw polygons of such a small area. No error message is displayed, but the polygons are invisible.