Fishing Effort in the Mexico EEZ


In [1]:
import bq  
import time
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import matplotlib
from matplotlib import colors,colorbar
import matplotlib
%matplotlib inline
import csv 
import math
from math import radians, cos, sin, asin, sqrt
from scipy import stats
import matplotlib.dates as mdates

client = bq.Client.Get()


/Users/David/Desktop/Jobs/GlobalFishingWatch/github/vessel-maps/utilities/pipa_paper/venv/lib/python2.7/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
  warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')

In [9]:
# create a bounding box:
max_lat = 32.396255
min_lat =  13.268187
max_lon = -85.232204
min_lon =  -118.825010
distance_from_shore_filter = 5 # in nautical miles
EEZ = 'Mexico'

In [10]:
def Query(q):
    t0 = time.time()
    answer = client.ReadTableRows(client.Query(q)['configuration']['query']['destinationTable'])
    print 'Query time: ' + str(time.time() - t0) + ' seconds.'
    return answer

In [11]:
q = '''
select count(distinct mmsi) 
FROM
  [tilesets.pipeline_2015_08_24_08_19_01]
WHERE
  latitude > '''+str(min_lat)+'''
  AND latitude <'''+str(max_lat)+'''
  AND longitude > '''+str(min_lon)+'''
  AND longitude < '''+str(max_lon)+'''
  AND weight >=.5
  AND JSON_EXTRACT(extra,"$.eez") = '"EEZ:'''+EEZ+'''"'
  and float(JSON_EXTRACT(EXTRA,"$.distance_to_shore")) > '''+str(distance_from_shore_filter)+'''
  '''
number_of_mmsi = Query(q)


select count(distinct mmsi) 
FROM
  [tilesets.pipeline_2015_08_24_08_19_01]
WHERE
  latitude > 13.268187
  AND latitude <32.396255
  AND longitude > -118.82501
  AND longitude < -85.232204
  AND weight >=.5
  AND JSON_EXTRACT(extra,"$.eez") = '"EEZ:Mexico"'
  and float(JSON_EXTRACT(EXTRA,"$.distance_to_shore")) > 5
  
Waiting on bqjob_r17e2bc745835d130_000001545f9b6243_4 ... (22s) Current status: DONE   
Query time: 25.1400258541 seconds.

In [12]:
print "Number of unique MMSI:",int(number_of_mmsi[0][0])


Number of unique MMSI: 171

171 MMSI were fishing in this region from January 2014 to July 2015


In [16]:
q = '''
select a.mmsi mmsi,
a.number number,
if(b.country is null, "invalid mmsi",b.country) country,
b.continent continent
from
(select mmsi, 
integer(if(length(string(mmsi))= 9,LEFT(STRING(mmsi),3), '0')) code,
count(*) number from
(SELECT
  mmsi,
  date(timestamp) date,
  first(latitude) lat,
  first(longitude) lon
FROM
  [tilesets.pipeline_2015_08_24_08_19_01]
WHERE
  latitude > '''+str(min_lat)+'''
  AND latitude <'''+str(max_lat)+'''
  AND longitude > '''+str(min_lon)+'''
  AND longitude < '''+str(max_lon)+'''
  AND weight >=.5
  AND JSON_EXTRACT(extra,"$.eez") = '"EEZ:'''+EEZ+'''"'
  and float(JSON_EXTRACT(EXTRA,"$.distance_to_shore")) > '''+str(distance_from_shore_filter)+'''


group by mmsi, date) 
group by mmsi, code 
) 
a
left join [scratch_roan.country_code] b
on a.code = b.code 
order by number desc'''

days_by_mmsi = Query(q)


Waiting on bqjob_r4152eaa731c831a2_000001545f9ccae8_6 ... (14s) Current status: DONE   
Query time: 17.5256698132 seconds.

The List of MMSI that were fishing are below


In [17]:
for r in days_by_mmsi:
    if "Taiwan" in r[2]:
        r[2] = "Taiwan"
    print "mmsi:", r[0],"  fishing days:", r[1], "  country: ", r[2].split(" (")[0]


mmsi: 367110910   fishing days: 210   country:  United States of America
mmsi: 345900429   fishing days: 190   country:  Mexico
mmsi: 123450800   fishing days: 129   country:  invalid mmsi
mmsi: 366749490   fishing days: 121   country:  United States of America
mmsi: 345050700   fishing days: 116   country:  Mexico
mmsi: 345080500   fishing days: 100   country:  Mexico
mmsi: 345900418   fishing days: 97   country:  Mexico
mmsi: 366547000   fishing days: 87   country:  United States of America
mmsi: 123450020   fishing days: 82   country:  invalid mmsi
mmsi: 345903304   fishing days: 76   country:  Mexico
mmsi: 345140500   fishing days: 61   country:  Mexico
mmsi: 345080032   fishing days: 59   country:  Mexico
mmsi: 345080028   fishing days: 54   country:  Mexico
mmsi: 345905454   fishing days: 54   country:  Mexico
mmsi: 345080700   fishing days: 51   country:  Mexico
mmsi: 345080900   fishing days: 50   country:  Mexico
mmsi: 367704000   fishing days: 48   country:  United States of America
mmsi: 345991311   fishing days: 44   country:  Mexico
mmsi: 345903272   fishing days: 43   country:  Mexico
mmsi: 345991333   fishing days: 43   country:  Mexico
mmsi: 345861280   fishing days: 41   country:  Mexico
mmsi: 345880613   fishing days: 41   country:  Mexico
mmsi: 345981093   fishing days: 39   country:  Mexico
mmsi: 345080035   fishing days: 39   country:  Mexico
mmsi: 345080029   fishing days: 38   country:  Mexico
mmsi: 367472410   fishing days: 38   country:  United States of America
mmsi: 345080022   fishing days: 37   country:  Mexico
mmsi: 345080011   fishing days: 34   country:  Mexico
mmsi: 345903658   fishing days: 32   country:  Mexico
mmsi: 345860501   fishing days: 32   country:  Mexico
mmsi: 345991318   fishing days: 31   country:  Mexico
mmsi: 345903112   fishing days: 28   country:  Mexico
mmsi: 345861978   fishing days: 27   country:  Mexico
mmsi: 345080010   fishing days: 27   country:  Mexico
mmsi: 345883660   fishing days: 26   country:  Mexico
mmsi: 345903552   fishing days: 25   country:  Mexico
mmsi: 345903652   fishing days: 24   country:  Mexico
mmsi: 345903169   fishing days: 23   country:  Mexico
mmsi: 345880338   fishing days: 23   country:  Mexico
mmsi: 345861246   fishing days: 21   country:  Mexico
mmsi: 345903403   fishing days: 20   country:  Mexico
mmsi: 345880204   fishing days: 20   country:  Mexico
mmsi: 378001272   fishing days: 19   country:  British Virgin Islands - United Kingdom of Great Britain and Northern Ireland
mmsi: 338162367   fishing days: 19   country:  United States of America
mmsi: 345861253   fishing days: 19   country:  Mexico
mmsi: 345903623   fishing days: 19   country:  Mexico
mmsi: 345903666   fishing days: 18   country:  Mexico
mmsi: 345903285   fishing days: 18   country:  Mexico
mmsi: 345860489   fishing days: 18   country:  Mexico
mmsi: 345903319   fishing days: 17   country:  Mexico
mmsi: 345901679   fishing days: 17   country:  Mexico
mmsi: 338097159   fishing days: 17   country:  United States of America
mmsi: 345905889   fishing days: 17   country:  Mexico
mmsi: 345860766   fishing days: 16   country:  Mexico
mmsi: 367561770   fishing days: 16   country:  United States of America
mmsi: 345880681   fishing days: 15   country:  Mexico
mmsi: 345904165   fishing days: 15   country:  Mexico
mmsi: 345880650   fishing days: 14   country:  Mexico
mmsi: 345902453   fishing days: 14   country:  Mexico
mmsi: 412440077   fishing days: 13   country:  China
mmsi: 345904243   fishing days: 13   country:  Mexico
mmsi: 345903703   fishing days: 11   country:  Mexico
mmsi: 345905646   fishing days: 11   country:  Mexico
mmsi: 367151530   fishing days: 10   country:  United States of America
mmsi: 345904997   fishing days: 10   country:  Mexico
mmsi: 123456789   fishing days: 10   country:  invalid mmsi
mmsi: 345904325   fishing days: 9   country:  Mexico
mmsi: 345861272   fishing days: 9   country:  Mexico
mmsi: 345860483   fishing days: 9   country:  Mexico
mmsi: 345903269   fishing days: 8   country:  Mexico
mmsi: 345991320   fishing days: 8   country:  Mexico
mmsi: 345903981   fishing days: 8   country:  Mexico
mmsi: 345905947   fishing days: 8   country:  Mexico
mmsi: 345991163   fishing days: 8   country:  Mexico
mmsi: 345991986   fishing days: 8   country:  Mexico
mmsi: 919919990   fishing days: 8   country:  invalid mmsi
mmsi: 345903195   fishing days: 7   country:  Mexico
mmsi: 345903290   fishing days: 7   country:  Mexico
mmsi: 345991270   fishing days: 7   country:  Mexico
mmsi: 345905059   fishing days: 7   country:  Mexico
mmsi: 345883486   fishing days: 6   country:  Mexico
mmsi: 345991241   fishing days: 6   country:  Mexico
mmsi: 345883241   fishing days: 6   country:  Mexico
mmsi: 345861251   fishing days: 6   country:  Mexico
mmsi: 345882050   fishing days: 6   country:  Mexico
mmsi: 345883703   fishing days: 6   country:  Mexico
mmsi: 345991954   fishing days: 6   country:  Mexico
mmsi: 345903913   fishing days: 6   country:  Mexico
mmsi: 123450100   fishing days: 5   country:  invalid mmsi
mmsi: 345980758   fishing days: 5   country:  Mexico
mmsi: 345903422   fishing days: 5   country:  Mexico
mmsi: 367010460   fishing days: 5   country:  United States of America
mmsi: 345991972   fishing days: 5   country:  Mexico
mmsi: 345861893   fishing days: 5   country:  Mexico
mmsi: 345905091   fishing days: 5   country:  Mexico
mmsi: 345861231   fishing days: 4   country:  Mexico
mmsi: 345883847   fishing days: 4   country:  Mexico
mmsi: 345990766   fishing days: 4   country:  Mexico
mmsi: 345991334   fishing days: 4   country:  Mexico
mmsi: 345904370   fishing days: 4   country:  Mexico
mmsi: 349052425   fishing days: 4   country:  invalid mmsi
mmsi: 345905056   fishing days: 4   country:  Mexico
mmsi: 345980776   fishing days: 4   country:  Mexico
mmsi: 367619920   fishing days: 4   country:  United States of America
mmsi: 345861221   fishing days: 4   country:  Mexico
mmsi: 345860463   fishing days: 4   country:  Mexico
mmsi: 345861223   fishing days: 4   country:  Mexico
mmsi: 345990774   fishing days: 4   country:  Mexico
mmsi: 247229700   fishing days: 4   country:  Italy
mmsi: 345860471   fishing days: 4   country:  Mexico
mmsi: 345981397   fishing days: 3   country:  Mexico
mmsi: 345860640   fishing days: 3   country:  Mexico
mmsi: 345991953   fishing days: 3   country:  Mexico
mmsi: 338126336   fishing days: 3   country:  United States of America
mmsi: 345861282   fishing days: 3   country:  Mexico
mmsi: 345904946   fishing days: 3   country:  Mexico
mmsi: 345904101   fishing days: 3   country:  Mexico
mmsi: 345905016   fishing days: 3   country:  Mexico
mmsi: 345905093   fishing days: 3   country:  Mexico
mmsi: 338096421   fishing days: 3   country:  United States of America
mmsi: 345880660   fishing days: 3   country:  Mexico
mmsi: 345990777   fishing days: 3   country:  Mexico
mmsi: 345905086   fishing days: 3   country:  Mexico
mmsi: 412523126   fishing days: 2   country:  China
mmsi: 345905032   fishing days: 2   country:  Mexico
mmsi: 345903824   fishing days: 2   country:  Mexico
mmsi: 345980934   fishing days: 2   country:  Mexico
mmsi: 345991965   fishing days: 2   country:  Mexico
mmsi: 775616000   fishing days: 2   country:  Venezuela
mmsi: 345990783   fishing days: 2   country:  Mexico
mmsi: 338164283   fishing days: 2   country:  United States of America
mmsi: 338172317   fishing days: 2   country:  United States of America
mmsi: 345990175   fishing days: 2   country:  Mexico
mmsi: 367620180   fishing days: 2   country:  United States of America
mmsi: 345880175   fishing days: 2   country:  Mexico
mmsi: 345904286   fishing days: 1   country:  Mexico
mmsi: 345991207   fishing days: 1   country:  Mexico
mmsi: 345903121   fishing days: 1   country:  Mexico
mmsi: 367378220   fishing days: 1   country:  United States of America
mmsi: 9103923   fishing days: 1   country:  invalid mmsi
mmsi: 345880903   fishing days: 1   country:  Mexico
mmsi: 345990926   fishing days: 1   country:  Mexico
mmsi: 367058903   fishing days: 1   country:  United States of America
mmsi: 345991269   fishing days: 1   country:  Mexico
mmsi: 412444222   fishing days: 1   country:  China
mmsi: 367089620   fishing days: 1   country:  United States of America
mmsi: 367421550   fishing days: 1   country:  United States of America
mmsi: 367640090   fishing days: 1   country:  United States of America
mmsi: 345840654   fishing days: 1   country:  Mexico
mmsi: 338046482   fishing days: 1   country:  United States of America
mmsi: 367482470   fishing days: 1   country:  United States of America
mmsi: 345980796   fishing days: 1   country:  Mexico
mmsi: 367485290   fishing days: 1   country:  United States of America
mmsi: 538090242   fishing days: 1   country:  Marshall Islands
mmsi: 345901103   fishing days: 1   country:  Mexico
mmsi: 412477041   fishing days: 1   country:  China
mmsi: 345881582   fishing days: 1   country:  Mexico
mmsi: 367054010   fishing days: 1   country:  United States of America
mmsi: 366557240   fishing days: 1   country:  United States of America
mmsi: 412470558   fishing days: 1   country:  China
mmsi: 412462323   fishing days: 1   country:  China
mmsi: 412462052   fishing days: 1   country:  China
mmsi: 367415880   fishing days: 1   country:  United States of America
mmsi: 431704490   fishing days: 1   country:  Japan
mmsi: 412477445   fishing days: 1   country:  China
mmsi: 345881771   fishing days: 1   country:  Mexico
mmsi: 416003802   fishing days: 1   country:  Taiwan
mmsi: 367637970   fishing days: 1   country:  United States of America
mmsi: 412330072   fishing days: 1   country:  China
mmsi: 345991934   fishing days: 1   country:  Mexico
mmsi: 367432010   fishing days: 1   country:  United States of America

In [18]:
# Now Group by Country
q = '''
SELECT 
country,
sum(number) number
from
(select a.mmsi mmsi,
a.number number,
if(b.country is null, "invalid mmsi",b.country) country,
b.continent continent
from
(select mmsi, 
integer(if(length(string(mmsi))= 9,LEFT(STRING(mmsi),3), '0')) code,
count(*) number from
(SELECT
  mmsi,
  date(timestamp) date,
  first(latitude) lat,
  first(longitude) lon
FROM
  [tilesets.pipeline_2015_08_24_08_19_01]
WHERE
  latitude > '''+str(min_lat)+'''
  AND latitude <'''+str(max_lat)+'''
  AND longitude > '''+str(min_lon)+'''
  AND longitude < '''+str(max_lon)+'''
  AND weight >=.5
  AND JSON_EXTRACT(extra,"$.eez") = '"EEZ:'''+EEZ+'''"'
  and float(JSON_EXTRACT(EXTRA,"$.distance_to_shore")) > '''+str(distance_from_shore_filter)+'''

  group by mmsi, date) 
group by mmsi, code 
) 
a
left join [scratch_roan.country_code] b
on a.code = b.code)
group by country
order by number desc'''

country_groups = Query(q)


Waiting on bqjob_r2fdef756974543bb_000001545f9d8d56_7 ... (14s) Current status: DONE   
Query time: 16.9225180149 seconds.

In [19]:
print "days\tcountry"
for c in country_groups:
    print c[1],'\t', c[0].split(" (")[0]


days	country
2211 	Mexico
600 	United States of America
239 	invalid mmsi
22 	China
19 	British Virgin Islands - United Kingdom of Great Britain and Northern Ireland
4 	Italy
2 	Venezuela
1 	Japan
1 	Marshall Islands
1 	Taiwan

In [20]:
objects = [c[0].split(" (")[0] for c in country_groups]
y_pos = np.arange(len(objects))
performance = [c[1] for c in country_groups]
 
plt.figure(figsize=(12,4))
plt.bar(y_pos-4, performance, align='center', alpha=0.5,)
plt.xticks(y_pos-4+.2, objects, fontsize=12) # rotation=45
locs, labels = plt.xticks()
plt.setp(labels, rotation=45,ha='right')
plt.ylabel('Fishing Days',fontsize=12)
plt.title('Fishing Days by Country in Mexico EEZ, Jan 2014 to July 2015',fontsize=15)

ax = plt.axes()
ax.xaxis.set_ticks_position('none') 
plt.savefig("fishing_effort_mexico_by_Country.png",bbox_inches='tight',dpi=150,transparent=True,pad_inches=.1)
plt.show()


Map the Fishing Effort in This Region


In [21]:
# Now Group by Country
q = '''
SELECT
lat,
lon,
count(*) fishing_days
from
(SELECT
  mmsi,
  date(timestamp) date,
  integer(first(latitude)*4) lat,
  integer(first(longitude)*4) lon
FROM
  [tilesets.pipeline_2015_08_24_08_19_01]
WHERE
  latitude > '''+str(min_lat)+'''
  AND latitude <'''+str(max_lat)+'''
  AND longitude > '''+str(min_lon)+'''
  AND longitude < '''+str(max_lon)+'''
  AND weight >=.5
  AND JSON_EXTRACT(extra,"$.eez") = '"EEZ:'''+EEZ+'''"'
  and float(JSON_EXTRACT(EXTRA,"$.distance_to_shore")) > '''+str(distance_from_shore_filter)+'''
  group by mmsi, date) 
group by lat, lon
'''

fishing_grid = Query(q)


Waiting on bqjob_r61913a779272070b_000001545f9e5703_8 ... (15s) Current status: DONE   
Query time: 17.7302289009 seconds.

In [22]:
cellsize = .25
one_over_cellsize = 4

num_lats = (max_lat-min_lat)*one_over_cellsize+1
num_lons = (max_lon-min_lon)*one_over_cellsize+1


grid = np.zeros(shape=(num_lats,num_lons))

for row in fishing_grid:
    lat = int(row[0])
    lon = int(row[1])
    lat_index = lat-min_lat*one_over_cellsize
    lon_index = lon-min_lon*one_over_cellsize
    grid[lat_index][lon_index] = int(row[2])


/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ipykernel/__main__.py:8: DeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ipykernel/__main__.py:15: DeprecationWarning: using a non-integer number instead of an integer will result in an error in the future

In [24]:
plt.rcParams["figure.figsize"] = [8,9]

firstlat = max_lat
lastlat =  min_lat
firstlon = min_lon
lastlon = max_lon
scale = cellsize

numlats = int((firstlat-lastlat)/scale+.5)
numlons = int((lastlon-firstlon)/scale+.5)
    
lat_boxes = np.linspace(lastlat,firstlat,num=numlats,endpoint=False)
lon_boxes = np.linspace(firstlon,lastlon,num=numlons,endpoint=False)

fig = plt.figure()
extra = 1
m = Basemap(llcrnrlat=lastlat-extra, urcrnrlat=firstlat+extra,
          llcrnrlon=firstlon-extra, urcrnrlon=lastlon+extra, lat_ts=0, projection='mill',resolution="h")

m.drawmapboundary(fill_color='#111111')
m.drawcoastlines(linewidth=.2)
m.fillcontinents('#cccccc',lake_color='#cccccc')#, lake_color, ax, zorder, alpha)

x = np.linspace(firstlon, lastlon, -(firstlon-lastlon)*one_over_cellsize+1)
y = np.linspace(lastlat, firstlat, (firstlat-lastlat)*one_over_cellsize+1)
x, y = np.meshgrid(x, y)
converted_x, converted_y = m(x, y)
from matplotlib import colors,colorbar

maximum = grid.max()
minimum = 1

norm = colors.LogNorm(vmin=minimum, vmax=maximum)
# norm = colors.Normalize(vmin=0, vmax=1000)

m.pcolormesh(converted_x, converted_y, grid, norm=norm, vmin=minimum, vmax=maximum, cmap = plt.get_cmap('viridis'))

t = "Fishing Days Jan 2014 to July 2015 \n in Mexico EEZ"
plt.title(t, color = "#000000", fontsize=18)

ax = fig.add_axes([0.2, 0.1, 0.65, 0.02]) #x coordinate , 
norm = colors.LogNorm(vmin=minimum, vmax=maximum)
# norm = colors.Normalize(vmin=0, vmax=1000)
lvls = np.logspace(np.log10(minimum),np.log10(maximum),num=8)
cb = colorbar.ColorbarBase(ax,norm = norm, orientation='horizontal', ticks=lvls, cmap = plt.get_cmap('viridis'))

#cb.ax.set_xticklabels(["0" ,round(m3**.5,1), m3, round(m3**1.5,1), m3*m3,round(m3**2.5,1), str(round(m3**3,1))+"+"], fontsize=10)
cb.ax.set_xticklabels([int(i) for i in lvls], fontsize=10, color = "#000000")
cb.set_label('Fishing Days',labelpad=-40, y=0.45, color = "#000000")
plt.savefig("fishing_effort_mexico.png",bbox_inches='tight',dpi=300,transparent=True,pad_inches=.1)
plt.show()



In [ ]:


In [59]:
# Now fishing days outside the EEZ
q = '''
SELECT
lat,
lon,
count(*) fishing_days
from
(SELECT
  mmsi,
  date(timestamp) date,
  integer(first(latitude)*4) lat,
  integer(first(longitude)*4) lon
FROM
  [tilesets.pipeline_2015_08_24_08_19_01]
WHERE
  latitude > '''+str(min_lat)+'''
  AND latitude <'''+str(max_lat)+'''
  AND longitude > '''+str(min_lon)+'''
  AND longitude < '''+str(max_lon)+'''
  AND weight >=.5
  AND JSON_EXTRACT(extra,"$.eez") != '"EEZ:'''+EEZ+'''"'
  and float(JSON_EXTRACT(EXTRA,"$.distance_to_shore")) > '''+str(distance_from_shore_filter)+'''
  group by mmsi, date) 
group by lat, lon
'''

fishing_grid = Query(q)


Waiting on bqjob_r4730e290db836f61_00000153e725fc5d_20 ... (15s) Current status: DONE   
Query time: 18.7124450207 seconds.

In [60]:
cellsize = .25
one_over_cellsize = 4

num_lats = (max_lat-min_lat)*one_over_cellsize+1
num_lons = (max_lon-min_lon)*one_over_cellsize+1


grid = np.zeros(shape=(num_lats,num_lons))

for row in fishing_grid:
    lat = int(row[0])
    lon = int(row[1])
    lat_index = lat-min_lat*one_over_cellsize
    lon_index = lon-min_lon*one_over_cellsize
    grid[lat_index][lon_index] = int(row[2])


/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ipykernel/__main__.py:8: DeprecationWarning: using a non-integer number instead of an integer will result in an error in the future

In [61]:
plt.rcParams["figure.figsize"] = [8,9]

firstlat = max_lat
lastlat =  min_lat
firstlon = min_lon
lastlon = max_lon
scale = cellsize

numlats = int((firstlat-lastlat)/scale+.5)
numlons = int((lastlon-firstlon)/scale+.5)
    
lat_boxes = np.linspace(lastlat,firstlat,num=numlats,endpoint=False)
lon_boxes = np.linspace(firstlon,lastlon,num=numlons,endpoint=False)

fig = plt.figure()
extra = 10
m = Basemap(llcrnrlat=lastlat-extra, urcrnrlat=firstlat+extra,
          llcrnrlon=firstlon-extra, urcrnrlon=lastlon+extra, lat_ts=0, projection='mill',resolution="h")

m.drawmapboundary(fill_color='#111111')
m.drawcoastlines(linewidth=.2)
m.fillcontinents('#cccccc',lake_color='#cccccc')#, lake_color, ax, zorder, alpha)

x = np.linspace(firstlon, lastlon, -(firstlon-lastlon)*one_over_cellsize+1)
y = np.linspace(lastlat, firstlat, (firstlat-lastlat)*one_over_cellsize+1)
x, y = np.meshgrid(x, y)
converted_x, converted_y = m(x, y)
from matplotlib import colors,colorbar

maximum = grid.max()
minimum = 1

norm = colors.LogNorm(vmin=minimum, vmax=maximum)
# norm = colors.Normalize(vmin=0, vmax=1000)

m.pcolormesh(converted_x, converted_y, grid, norm=norm, vmin=minimum, vmax=maximum, cmap = plt.get_cmap('viridis'))

t = "Fishing Days Jan 2014 to July 2015 \n Outside Seychelles EEZ"
plt.title(t, color = "#000000", fontsize=18)

ax = fig.add_axes([0.2, 0.1, 0.65, 0.02]) #x coordinate , 
norm = colors.LogNorm(vmin=minimum, vmax=maximum)
# norm = colors.Normalize(vmin=0, vmax=1000)
lvls = np.logspace(np.log10(minimum),np.log10(maximum),num=8)
cb = colorbar.ColorbarBase(ax,norm = norm, orientation='horizontal', ticks=lvls, cmap = plt.get_cmap('viridis'))

#cb.ax.set_xticklabels(["0" ,round(m3**.5,1), m3, round(m3**1.5,1), m3*m3,round(m3**2.5,1), str(round(m3**3,1))+"+"], fontsize=10)
cb.ax.set_xticklabels([int(i) for i in lvls], fontsize=10, color = "#000000")
cb.set_label('Fishing Days',labelpad=-40, y=0.45, color = "#000000")
plt.savefig("fishing_effort_outside_seychelles.png",bbox_inches='tight',dpi=300,transparent=True,pad_inches=.1)
plt.show()



In [ ]:


In [62]:
# Now fishing days outside the EEZ
q = '''
SELECT
lat,
lon,
count(*) fishing_days
from
(SELECT
  mmsi,
  date(timestamp) date,
  integer(first(latitude)*4) lat,
  integer(first(longitude)*4) lon
FROM
  [tilesets.pipeline_2015_08_24_08_19_01]
WHERE
  latitude > '''+str(min_lat)+'''
  AND latitude <'''+str(max_lat)+'''
  AND longitude > '''+str(min_lon)+'''
  AND longitude < '''+str(max_lon)+'''
  AND weight >=.5
  //AND JSON_EXTRACT(extra,"$.eez") != '"EEZ:'''+EEZ+'''"'
  and float(JSON_EXTRACT(EXTRA,"$.distance_to_shore")) > '''+str(distance_from_shore_filter)+'''
  group by mmsi, date) 
group by lat, lon
'''

fishing_grid = Query(q)


Waiting on bqjob_r409a8f6c2cc2393c_00000153e740c1c4_21 ... (23s) Current status: DONE   
Query time: 27.3496830463 seconds.

In [63]:
cellsize = .25
one_over_cellsize = 4

num_lats = (max_lat-min_lat)*one_over_cellsize+1
num_lons = (max_lon-min_lon)*one_over_cellsize+1


grid = np.zeros(shape=(num_lats,num_lons))

for row in fishing_grid:
    lat = int(row[0])
    lon = int(row[1])
    lat_index = lat-min_lat*one_over_cellsize
    lon_index = lon-min_lon*one_over_cellsize
    grid[lat_index][lon_index] = int(row[2])


/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ipykernel/__main__.py:8: DeprecationWarning: using a non-integer number instead of an integer will result in an error in the future

In [64]:
plt.rcParams["figure.figsize"] = [8,9]

firstlat = max_lat
lastlat =  min_lat
firstlon = min_lon
lastlon = max_lon
scale = cellsize

numlats = int((firstlat-lastlat)/scale+.5)
numlons = int((lastlon-firstlon)/scale+.5)
    
lat_boxes = np.linspace(lastlat,firstlat,num=numlats,endpoint=False)
lon_boxes = np.linspace(firstlon,lastlon,num=numlons,endpoint=False)

fig = plt.figure()
extra = 10
m = Basemap(llcrnrlat=lastlat-extra, urcrnrlat=firstlat+extra,
          llcrnrlon=firstlon-extra, urcrnrlon=lastlon+extra, lat_ts=0, projection='mill',resolution="h")

m.drawmapboundary(fill_color='#111111')
m.drawcoastlines(linewidth=.2)
m.fillcontinents('#cccccc',lake_color='#cccccc')#, lake_color, ax, zorder, alpha)

x = np.linspace(firstlon, lastlon, -(firstlon-lastlon)*one_over_cellsize+1)
y = np.linspace(lastlat, firstlat, (firstlat-lastlat)*one_over_cellsize+1)
x, y = np.meshgrid(x, y)
converted_x, converted_y = m(x, y)
from matplotlib import colors,colorbar

maximum = grid.max()
minimum = 1

norm = colors.LogNorm(vmin=minimum, vmax=maximum)
# norm = colors.Normalize(vmin=0, vmax=1000)

m.pcolormesh(converted_x, converted_y, grid, norm=norm, vmin=minimum, vmax=maximum, cmap = plt.get_cmap('viridis'))

t = "Fishing Days Jan 2014 to July 2015 \n Around Seychelles"
plt.title(t, color = "#000000", fontsize=18)

ax = fig.add_axes([0.2, 0.1, 0.65, 0.02]) #x coordinate , 
norm = colors.LogNorm(vmin=minimum, vmax=maximum)
# norm = colors.Normalize(vmin=0, vmax=1000)
lvls = np.logspace(np.log10(minimum),np.log10(maximum),num=8)
cb = colorbar.ColorbarBase(ax,norm = norm, orientation='horizontal', ticks=lvls, cmap = plt.get_cmap('viridis'))

#cb.ax.set_xticklabels(["0" ,round(m3**.5,1), m3, round(m3**1.5,1), m3*m3,round(m3**2.5,1), str(round(m3**3,1))+"+"], fontsize=10)
cb.ax.set_xticklabels([int(i) for i in lvls], fontsize=10, color = "#000000")
cb.set_label('Fishing Days',labelpad=-40, y=0.45, color = "#000000")
plt.savefig("fishing_effort_outside_and_indside_seychelles.png",bbox_inches='tight',dpi=300,transparent=True,pad_inches=.1)
plt.show()



In [ ]: