SpellBook

A SpellBook is a collection of spells that can be sequentially casted and merged in a single dataframe. This is useful if you have a feature-collection that you want to reuse or share to other people:

from geomancer.spells import DistanceToNearest, NumberOf
from geomancer.spellbook import SpellBook
from tests.conftest import sample_points


# Create a spellbook
spellbook = SpellBook(
             spells=[
                 DistanceToNearest("primary",
                                    source_table="geospatial.ph_osm.gis_osm_roads_free_1",
                                    feature_name="dist_primary"),
                 NumberOf("supermarket"
                           source_table="geospatial.ph_osm.gis_osm_pois_free_1",
                           feature_name="num_supermarkets"),
             ])

SpellBooks can be distributed by exporting them to JSON files.

# Export SpellBook into a file
spellbook.author = "Juan dela Cruz"
spellbook.description = "Good Features for Economic Indicators"
spellbook.to_json("features_dela_cruz.json")

Now other people can easily reuse your feature extractions in with their own datasets!

from geomancer.spellbook import SpellBook
from tests.conftest import sample_points

spellbook = SpellBook.read_json("features_dela_cruz.json")
df = sample_points() # load your own data

# Cast someone's Spells into your own data
df_with_features = spellbook.cast(df)

geomancer.spellbook.SpellBook

class geomancer.spellbook.spellbook.SpellBook(spells, column='WKT', author=None, description=None)[source]

Bases: object

__init__(spells, column='WKT', author=None, description=None)[source]

SpellBook constructor

Parameters:
  • spells (list of geomancer.spells.Spell) – List of spell instances.
  • column (str, optional) – Column to look the geometries from. The default is WKT
  • author (str, optional) – Author of the spell book
  • description (str, optional) – Description of the spell book
cast(df)[source]

Runs the cast method of each spell in the spell book

Parameters:df (pandas.DataFrame) – Dataframe containing the points to compare upon. By default, we will look into the geometry column. You can specify your own column by passing an argument to the column parameter.
Returns:Output dataframe with the features from all spells
Return type:pandas.DataFrame
classmethod read_json(filename)[source]

Reads a JSON exported spell book

Parameters:filename (str) – Filename of JSON file to read.
Returns:SpellBook instance parsed from given JSON file.
Return type:geomancer.spellbook.SpellBook
to_json(filename=None, **kwargs)[source]

Exports spell book as a JSON string

Parameters:filename (str, optional) – Output filename. If none is given, output is returned
Returns:Export of spell book in JSON format
Return type:str or None