from learntools.core import *

import geopandas as gpd

congrats_map_completion = "Thank you for creating a map!"
correct_message_map_completion = ""

# Q1
world_loans = gpd.read_file("../input/geospatial-learn-course-data/kiva_loans/kiva_loans/kiva_loans.shp")

# Q3
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
PHL = world.loc[world.iso_a3=="PHL"].copy()
PHL_loans = world_loans.loc[world_loans.country=="Philippines"].copy()

class Q1(CodingProblem):
    _var = "world_loans"
    _hint = "Use `gpd.read_file()`."
    _solution = CS(
"""# Load the data
world_loans = gpd.read_file(loans_filepath)
""")
    def check(self, world_loans_submit):
        assert world_loans_submit.shape == world_loans.shape, "The results don't look right.  Please try again."

class Q2(CodingProblem):
    _hint = "Use the `plot()` method of each GeoDataFrame."
    _solution = CS(
"""ax = world.plot(figsize=(20,20), color='whitesmoke', linestyle=':', edgecolor='black')
world_loans.plot(ax=ax, markersize=2)
""")
    _congrats = congrats_map_completion
    _correct_message = correct_message_map_completion
    def check(self):
        pass 

class Q3(EqualityCheckProblem):
    _var = "PHL_loans"
    _expected = PHL_loans
    _hint = ("Check out [this tutorial](https://www.kaggle.com/residentmario/indexing-selecting-assigning/), "
             "if you need to review conditional selection for DataFrames (and GeoDataFrames).")
    _solution = CS(
"""PHL_loans = world_loans.loc[world_loans.country=="Philippines"].copy()
""")

class Q4P(CodingProblem):
    _hint = "Use the `plot()` method of each GeoDataFrame."
    _solution = CS(
"""ax = PHL.plot(figsize=(12,12), color='whitesmoke', linestyle=':', edgecolor='lightgray')
PHL_loans.plot(ax=ax, markersize=2)
""")
    _congrats = congrats_map_completion
    _correct_message = correct_message_map_completion
    def check(self):
        pass
    
class Q4T(ThoughtExperiment):
    _hint = "Take a look at the map above.  Do all islands have loans?"
    _solution = ("There are a number of potential islands, but **Mindoro** (in the "
                 "central part of the Philippines) stands out as a relatively large island without any loans "
                 "in the current dataset.  This island is potentially a good location for "
                 "recruiting new Field Partners!")
    
Q4 = MultipartProblem(Q4P, Q4T)

qvars = bind_exercises(globals(), [
    Q1, Q2, Q3, Q4
    ],
    var_format='q_{n}',
    )
__all__ = list(qvars)

@@ -47,16 +47,36 @@
 s as gpd
+%0Aimport pandas as pd
 %0A%0Acongra
@@ -164,16 +164,460 @@
 n = %22%22%0A%0A
+def check_gdf_equal(gdf1, gdf2, name):%0A    assert type(gdf1)==gpd.geodataframe.GeoDataFrame, %22%60%7B%7D%60 is not a GeoDataFrame.%22.format(name)%0A    df1 = pd.DataFrame(gdf1.drop(columns='geometry'))%0A    df2 = pd.DataFrame(gdf2.drop(columns='geometry'))%0A    assert df1==df2, %22The results don't look right.  Try again.%22%0A    geom1 = gdf1.geometry%0A    geom2 = gdf2.geometry%0A    assert geom1.equals(geom2).all(), %22The results don't look right.  Try again.%22%0A%0A
 # Q1%0Awor
@@ -1133,23 +1133,32 @@
         
-assert 
+check_gdf_equal(
 world_lo
@@ -1171,17 +1171,9 @@
 bmit
-.shape ==
+,
  wor
@@ -1184,66 +1184,24 @@
 oans
-.shape, %22The results don't look right.  Please try again.%22
+, %22world_loans%22)
 %0A%0Acl

Update ex1.py