<jupyter_start><jupyter_text>Extracting CMA (Census Metropolitan Area) Boundaries Process- Load Canadian Census Geometries- Extract boundaries for each city- Extraxt DAs for each CMA- Save Datahttps://www.statcan.gc.ca/eng/subjects/standard/daily/5000076<jupyter_code>import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
import os
os.chdir('C:/Users/Leonardo/Documents/MetroWork/RealEstateData')
canada = gpd.read_file('CensusData2016/Geographies/lda_000b16a_e.shp')<jupyter_output><empty_output><jupyter_text>Logic for study areasAll canadian metropolitan areas greater than 1 million peoplehttps://en.wikipedia.org/wiki/Template:Largest_metropolitan_areas_of_Canada<jupyter_code>#function for extracting metropolitan census blocks
def get_metrocensus(canada=canada, MAID=[], MANAMES=[], PROVFILTER=[]): 
    '''filters canadian census layer by metropolitan area, The result is census blocks for a given metropolitan area'''
    #filter by metropolitan area
    if MAID != []:
        MAREA = canada[canada.CMAPUID.isin(MAID)]
        return MAREA
    elif MAID == []:
        MAREA = canada[canada.CDNAME.isin(MANAMES) & ~canada.PRUID.isin(PROVFILTER)]
        return MAREA
#function for extracting metropolitan boundaries
def get_metrobounds(canada=canada, MAID=[], MANAMES=[], PROVFILTER=[]): 
    '''filters canadian census layer by metropolitan area, and dissolves all polygons. The result is metropolitan boundaries'''
    #create new col to be used with dissolve
    canada['COUNTRY'] = 1
    if MAID != []:
        MAREA = canada[canada.CMAPUID.isin(MAID)]
        MAREA = MAREA.dissolve(by='COUNTRY')
        MAREA = MAREA[['CMANAME', 'geometry']]
        return MAREA
    elif MAID == []:
        MAREA = canada[canada.CDNAME.isin(MANAMES) & ~canada.PRUID.isin(PROVFILTER)]
        MAREA = MAREA.dissolve(by='COUNTRY')
        MAREA = MAREA[['CMANAME', 'geometry']]
        return MAREA   
## BLOCKS
#get metro toronto
MTBLOCKS = get_metrocensus(canada=canada, MANAMES=['Toronto', 'Hamilton', 'Durham', 'Halton', 'Peel', 'York'], PROVFILTER=['13'])
MTBLOCKS = MTBLOCKS.to_crs({'init' :'epsg:4326'})
# MTBLOCKS.to_file('CensusData2016_MA/CensusBlocks/MTBLOCKS.shp')
#get metro vancouver
MVBLOCKS = get_metrocensus(canada=canada, MAID=['59933'])
MVBLOCKS = MVBLOCKS.to_crs({'init' :'epsg:4326'})
# MVBLOCKS.to_file('CensusData2016_MA/CensusBlocks/MVBLOCKS.shp')
##statscan says that metro toronto is this: "CMAPUID" IN ('35535') but it doesnt make sense.. investigate!
#get metro montreal
MMBLOCKS = get_metrocensus(canada=canada, MAID=['24462'])
MMBLOCKS = MMBLOCKS.to_crs({'init' :'epsg:4326'})
# MMBLOCKS.to_file('CensusData2016_MA/CensusBlocks/MMBLOCKS.shp')
#get Ottawa-Gattineau
MOBLOCKS = get_metrocensus(canada=canada, MAID=['35505', '24505'])
MOBLOCKS = MOBLOCKS.to_crs({'init' :'epsg:4326'})
# MOBLOCKS.to_file('CensusData2016_MA/CensusBlocks/MOBLOCKS.shp')
#get metro calgary
MCBLOCKS = get_metrocensus(canada=canada, MAID=['48825'])
MCBLOCKS = MCBLOCKS.to_crs({'init' :'epsg:4326'})
# MCBLOCKS.to_file('CensusData2016_MA/CensusBlocks/MCBLOCKS.shp')
#get metro edmonton
MEBLOCKS = get_metrocensus(canada=canada, MAID=['48835'])
MEBLOCKS = MEBLOCKS.to_crs({'init' :'epsg:4326'})
# MEBLOCKS.to_file('CensusData2016_MA/CensusBlocks/MEBLOCKS.shp')
# BOUNDARIES
#get metro toronto
MTAREA = get_metrobounds(canada=canada, MANAMES=['Toronto', 'Hamilton', 'Durham', 'Halton', 'Peel', 'York'], PROVFILTER=['13'])
MTAREA = MTAREA.to_crs({'init' :'epsg:4326'})
MTAREA.to_file('Boundaries/MTAREA.shp')
#get metro vancouver
MVAREA = get_metrobounds(canada=canada, MAID=['59933'])
MVAREA = MVAREA.to_crs({'init' :'epsg:4326'})
MVAREA.to_file('Boundaries/MVAREA.shp')
#get metro montreal
MMAREA = get_metrobounds(canada=canada, MAID=['24462'])
MMAREA = MMAREA.to_crs({'init' :'epsg:4326'})
MMAREA.to_file('Boundaries/MMAREA.shp')
#get Ottawa-Gattineau
MOAREA = get_metrobounds(canada=canada, MAID=['35505', '24505'])
MOAREA = MOAREA.to_crs({'init' :'epsg:4326'})
MOAREA.to_file('Boundaries/MOAREA.shp')
#get metro calgary
MCAREA = get_metrobounds(canada=canada, MAID=['48825'])
MCAREA = MCAREA.to_crs({'init' :'epsg:4326'})
MCAREA.to_file('Boundaries/MCAREA.shp')
#get metro edmonton
MEAREA = get_metrobounds(canada=canada, MAID=['48835'])
MEAREA = MEAREA.to_crs({'init' :'epsg:4326'})
MEAREA.to_file('Boundaries/MEAREA.shp')
MVAREA.plot()
MTAREA.plot()
MMAREA.plot()
MOAREA.plot()
MCAREA.plot()
MEAREA.plot()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15006699419383654, 'fraction_numerical': 0.03550692273336311, 'mean_word_length': 6.342622950819672, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '111366', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1947, 'n_tokens_neox': 1747, 'n_words': 310, 'path': '1_CityBoundaries.ipynb', 'repo_name': 'lnicoletti/livability-equity-canada', 'size': '4478'}