Drawing a square in Cartopy

2,951 views
Skip to first unread message

Jamie Mitchell

unread,
Jul 11, 2014, 8:17:07 AM7/11/14
to scitoo...@googlegroups.com
Hello all,

I want to draw a square over the following lat/lon points in cartopy:

lat1 = 50.27, lat2=50.48, lon1=-5.5, lon2=-5.8.

This is my code so far:

python2.7
import matplotlib.pyplot as plt
from matplotlib.transforms import offset_copy
import cartopy.crs as ccrs
import cartopy.io.img_tiles as cimgt

def main():

  map_quest_aerial=cimgt.MapQuestOpenAerial()
  ax=plt.axes(projection=map_quest_aerial.crs)
  ax.set_extent([-4,-6,50,51])
  ax.add_image(map_quest_aerial, 8)
  plt.show()

if __name__ == '__main__':
    main()

This code displays the larger general area that I want to show but I don't know how to draw a square around the specific lon/lat points?

Thanks,

Jamie

bjlittle

unread,
Jul 11, 2014, 9:10:49 AM7/11/14
to scitoo...@googlegroups.com
Hi Jamie,

Is this what your after ... see attached figure generated by the following:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.io.img_tiles as cimgt

from shapely.geometry.polygon import LinearRing


def main():
  map_quest_aerial
= cimgt.MapQuestOpenAerial()
  ax
= plt.axes(projection=map_quest_aerial.crs)
  ax
.set_extent([-4, -6, 50, 51], ccrs.PlateCarree())
  ax
.add_image(map_quest_aerial, 8)

  lons
= [-5.8, -5.8, -5.5, -5.5]
  lats
= [50.27, 50.48, 50.48, 50.27]
  ring
= LinearRing(list(zip(lons, lats)))
  ax
.add_geometries([ring], ccrs.PlateCarree(), facecolor='none', edgecolor='white')


  plt
.show()


if __name__ == '__main__':
    main
()

HTH
-Bill

figure_1.png

Jamie Mitchell

unread,
Jul 11, 2014, 11:00:47 AM7/11/14
to scitoo...@googlegroups.com
bjlittle that is exactly what I needed thank you very much!

Jamie Mitchell

unread,
Jul 11, 2014, 11:16:29 AM7/11/14
to scitoo...@googlegroups.com
Hi bjlittle,

The code that you provided worked for that example perfectly.

However when I tried to do the same thing for a different region it wouldn't create a square (please see attachment).

Here is the code:


import matplotlib.pyplot as plt
from matplotlib.transforms import offset_copy
import cartopy.crs as ccrs
import cartopy.io.img_tiles as cimgt
from shapely.geometry.polygon import LinearRing

def main():

  map_quest_aerial=cimgt.MapQuestOpenAerial()
  ax=plt.axes(projection=map_quest_aerial.crs)
  ax.set_extent([-6,-8,57,59], ccrs.PlateCarree())
  ax.add_image(map_quest_aerial, 8)
  plt.plot(-7.017,58.311, marker='o', color='yellow', markersize=12, alpha=0.7, transform=ccrs.Geodetic())
  geodetic_transform=ccrs.Geodetic()._as_mpl_transform(ax)
  text_transform=offset_copy(geodetic_transform, units='dots', x=-25)
  plt.text(-7.017, 58.311, u'Bernera Wave Site', verticalalignment='center', horizontalalignment='right', transform=text_transform, bbox=dict(facecolor='wheat', alpha=0.5, boxstyle='round'))

  lons= [-7.3,-7.3,-7,-7]
  lats=[58.4,58.61,58.4,58.61]
  ring = LinearRing(list(zip(lons,lats)))

  ax.add_geometries([ring], ccrs.PlateCarree(), facecolor='none', edgecolor='white')
  plt.show()

if __name__ == '__main__':
    main()

Do you know why this might be happening?

Thanks,

Jamie
bernera_wave_site.png

Andrew Dawson

unread,
Jul 11, 2014, 11:28:40 AM7/11/14
to scitoo...@googlegroups.com
Jamie

You've got your latitude points in the wrong order, that's all. Swap the last two latitudes over:

lats = [58.4 58.61, 58.61, 58.4]

In bjlittle's example the points went lower-left, upper-left, upper-right, lower-right. Yours go lower-left, upper-left, lower-right, upper-right, which defines the weird shape you are seeing.

Jamie Mitchell

unread,
Jul 11, 2014, 11:32:13 AM7/11/14
to scitoo...@googlegroups.com
Oh dear what a rookie mistake! Thanks Andrew
Reply all
Reply to author
Forward
0 new messages