How to Map the World's Organic Farms & Farmer's Markets with Python and OpenStreetMap
Introduction
Let's talk about food production and food distribution by exploring 2 great and socially impactful data sets. We'll start by looking at the percentage of farming area dedicated to organic agriculture around the world and will follow with the location of farmer's markets around the United States.
Code
from IPython.display import Image
Image(filename='who-produces-good-food.png', width='80%')
Healthy Food - World Agriculture - Organic VS Non & Locations of Farmers Markets in the US and¶
Let's talk about food production and food distribution by exploring 2 great and socially impactful data sets. We'll start by looking at the percentage of farming area dedicated to organic agriculture around the world and will follow with the location of farmer's markets around the United States.
Food is a big deal and can tell us a lot about how we think about health, the amount of money we're willing to spend on diets and how we support our local economies.
While we explore those data sets, I'll also show you how a cool function using openstreetmap to easily get geo coordinates using a country's name and how to get some quick stats by leveraging the ubiquitous pandas "groupby" function. Are you ready?
Welcome to the ViralML Show, my name is manuel amunategui, your host and promoter of pporting ml to the web so everybody gets to enjoy what we do, not just those that can navigate a Jupyter notbook. Sign up for my newlsetter and give this video some love.
Have you heard the term "Food Desert"? It's not a misspelling.¶
The United States Government defines a food desert as “a low income census tract where a substantial number or share of residents has low access to a supermarket or large grocery store.”
This is a big deal, rural areas and poor urban neighborhoods don't have access to quality foods especially fruits and vegetables and instead are stuck with high-calorie food sources such as junk food and fast food. That can lead to health issues such as diabetes and to those who have a disease, very difficult to manage them with poor food offerings.
Global Organic Farming¶
"There is no universally accepted definition of organic farming , but most consider it to be a specific production system that aims to avoid the use of synthetic and harmful pesticides, fertilizers, growth regulators, and livestock feed additives." https://www.thebalancesmb.com/the-definition-of-organic-farming-2538081
Organic Standards were published in 2002. And requires the producer to register and be certified on a regular basis that they are following stantarrds...
Whether you don't like pesticides or genetically modified seeds in your food or your environment is besides the point. Organic farming is over 2x more profitable for farmers and it as a much lower impact on the enviroment. It also requires more labour thus can have a huge impact on the farming community.
"Producers, distributors and marketers of organic products must register with their local control body before they are allowed to market their food as organic after they have been inspected and checked, they will be awarded a certificate to confirm that their products meet organic standards all operators are checked at least once a year to make sure that they are continuing to follow the rules" https://ec.europa.eu/info/food-farming-fisheries/farming/organic-farming/organics-glance_en
Image(filename='Organic_farming_area_EU_and_EFTA.rev.jpg', width='60%')
Global Farming Land - Percentage Allocated to Ogranic Farming¶
Research Institute of Organic Agriculture
https://www.fibl.org/en/themes/statistics-info.html
Organic agriculture world-wide and in Europe¶
(source: https://www.fibl.org/en/themes/statistics-info.html)
"The main results of the latest survey on certified organic agriculture world-wide show (data end of 2017) that nearly 70 million hectares of agricultural land are managed organically. Growth was noted for all important indicators: Area, producers and retail sales.
On a global level, the organic agricultural land area increased by 11.7 million hectares or 20 percent compared with 2016.
The highest shares of organic agricultural land are in Liechtenstein (37.9 percent) and Samoa (37.6 percent).
There were over 2.8 million producers, and the countries with the highest numbers of producers are India, Uganda and Mexico.
The market research company Ecovia Intelligence estimates the global market for organic food to have reached 97 billion US dollars in 2017 (approximately 90 billion euros). The United States is the leading market with 40 billion euros, followed by Germany (10 billion euros), France (7.9 billion euros), and China (7.6 billion euros)."
# FIBL Research Institute of Organic Agriculture
import pandas as pd
glb_organic_df = pd.read_csv('global-organic-area-2017.csv', sep='\t')
glb_organic_df.head()
glb_organic_df.shape
# https://gis.stackexchange.com/questions/212796/get-lat-lon-extent-of-country-from-name-using-python
def get_boundingbox_country(country, output_as='boundingbox'):
"""
get the bounding box of a country in EPSG4326 given a country name
Parameters
----------
country : str
name of the country in english and lowercase
output_as : 'str
chose from 'boundingbox' or 'center'.
- 'boundingbox' for [latmin, latmax, lonmin, lonmax]
- 'center' for [latcenter, loncenter]
Returns
-------
output : list
list with coordinates as str
"""
import requests
# create url
url = '{0}{1}{2}'.format('http://nominatim.openstreetmap.org/search?country=',
country,
'&format=json&polygon=0')
response = requests.get(url).json()[0]
# parse response to list
if output_as == 'boundingbox':
lst = response[output_as]
output = [float(i) for i in lst]
if output_as == 'center':
lst = [response.get(key) for key in ['lon','lat']]
output = [float(i) for i in lst]
return output
import time
ctry_geo_centers_lon = []
ctry_geo_centers_lat = []
total_ctry = len(glb_organic_df['Country'])
counter_ = 0
for ctry in glb_organic_df['Country']:
if counter_ % 10 == 0: print(total_ctry - counter_)
time.sleep(0.2)
centroid = [None, None]
try:
centroid = get_boundingbox_country(ctry, output_as='center')
except:
print('Could not find:', ctry)
ctry_geo_centers_lon.append(centroid[0])
ctry_geo_centers_lat.append(centroid[1])
counter_ += 1
glb_organic_df['longitude'] = ctry_geo_centers_lon
glb_organic_df['latitude'] = ctry_geo_centers_lat
glb_organic_df = glb_organic_df.dropna()
# clean up Organic area share of total farmland [%]
glb_organic_df = glb_organic_df[pd.to_numeric(glb_organic_df['Organic area share of total farmland [%]'], errors='coerce').notnull()]
# cast 'Organic area share of total farmland [%]' col to numeric
glb_organic_df['Organic area share of total farmland [%]'] = glb_organic_df['Organic area share of total farmland [%]'].astype(float)
glb_organic_df.head()
glb_organic_df = glb_organic_df.sort_values('Organic area share of total farmland [%]', ascending=False)
glb_organic_df
# you will need to pip install Basemap - https://matplotlib.org/basemap/users/installing.html
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
plt.figure(figsize=(16, 8))
# Make the background map
m=Basemap(llcrnrlon=-180, llcrnrlat=-65,urcrnrlon=180,urcrnrlat=80)
m.drawmapboundary(fill_color='#A6CAE0', linewidth=0)
m.fillcontinents(color='grey', alpha=0.3)
m.drawcoastlines(linewidth=0.1, color="white")
# prepare a color for each point depending on the continent.
glb_organic_df['labels_enc'] = pd.factorize(glb_organic_df['Country'])[0]
# Add a point per position
m.scatter(glb_organic_df['longitude'], glb_organic_df['latitude'],
s=glb_organic_df['Organic area share of total farmland [%]']*100, alpha=0.4,
c=glb_organic_df['labels_enc'], cmap="Set1")
plt.title('Organic area share of total farmland [%]' , fontsize=50)
Farmer's Market¶
know your food, farmer, buy local, buy from small farmers - supporting local agriculture. This doesn't mean the food is certified organic (for those producing more than $5,000 worth of products annually) or that that it's pescticide free but it is supporting your local economy and those that chose to work the land.
Farmers Markets Directory and Geographic Data¶
https://catalog.data.gov/dataset/farmers-markets-geographic-data
Longitude and latitude, state, address, name, and zip code of Farmers Markets in the United States
import pandas as pd
farmers_market = pd.read_csv('Farmers Markets Directory and Geographic Data.csv')
farmers_market.head()
list(farmers_market)
farmers_market.shape
# raw plot of geo-coordinates
# map the ip addresses
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(16, 8))
ax.set_facecolor('xkcd:black')
plt.plot(farmers_market['x'],
farmers_market['y'],
linestyle='none', marker='.', color='white')
plt.title('US Farmers Market', fontsize=30)
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.grid()
plt.show()
farmers_market_tmp = farmers_market[(farmers_market['x'] > -130)
& (farmers_market['x'] < -50)
& (farmers_market['y'] > 25)]
fig, ax = plt.subplots(figsize=(16, 8))
ax.set_facecolor('xkcd:black')
plt.plot(farmers_market_tmp['x'],
farmers_market_tmp['y'],
linestyle='none', marker='.', color='white')
plt.title('US Farmers Market', fontsize=30)
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.grid()
plt.show()
# groupby Latitude
farmers_market_tmp = farmers_market_tmp[['FMID','x']]
farmers_market_tmp['x'] = farmers_market_tmp['x'].round(0)
farmers_market_tmp = farmers_market_tmp.sort_values('x')
farmers_market_rez = farmers_market_tmp[['FMID','x']].groupby('x').count().rename(columns={'FMID':'FMID_counts'}).reset_index()
farmers_market_rez.shape
farmers_market_rez.head()
# plot historgram
fig, ax = plt.subplots(figsize=(16, 8))
# plt.plot(farmers_market_rez['x'], farmers_market_rez['FMID_counts'])
plt.bar(farmers_market_rez['x'], farmers_market_rez['FMID_counts'], align='center', alpha=0.5)
plt.grid()
plt.xlabel('Longitude')
plt.ylabel('Count')
plt.title('Farmers Market Count by Longitude', fontsize=30)
Image(filename='1200px-Map_of_USA_with_state_names.png', width='90%')
# souce wikipedia commons
# https://commons.wikimedia.org/wiki/File:Map_of_USA_with_state_names.svg
Show Notes
(pardon typos and formatting -these are the notes I use to make the videos)
Let's talk about food production and food distribution by exploring 2 great and socially impactful data sets. We'll start by looking at the percentage of farming area dedicated to organic agriculture around the world and will follow with the location of farmer's markets around the United States.