Skip to content

Commit

Permalink
Merge pull request #678 from CodeForPhilly/lebovits/issu31-add-neighb…
Browse files Browse the repository at this point in the history
…oring-properties-count

Lebovits/issu31 add neighboring properties count
  • Loading branch information
nlebovits committed Jun 5, 2024
2 parents ebe559a + 7c94b46 commit d09fda3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
2 changes: 2 additions & 0 deletions data/src/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ data-diff = {extras = ["postgresql"], version = "*"}
future = "*"
slack-sdk = "*"
pytest = "*"
networkx = "*"
libpysal = "*"
jenkspy = "*"

[dev-packages]
Expand Down
19 changes: 9 additions & 10 deletions data/src/classes/featurelayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,34 @@
import geopandas as gpd
import pandas as pd
import requests
from config.config import FORCE_RELOAD, USE_CRS
from config.psql import conn
from esridump.dumper import EsriDumper
from google.cloud import storage
from google.cloud.storage.bucket import Bucket
from shapely import Point, wkb

from config.config import FORCE_RELOAD, USE_CRS


# Configure Google
def google_cloud_bucket() -> Bucket:
"""Build the google cloud bucket with name configured in your environ or default of cleanandgreenphl
Returns:
Bucket: the gcp bucket
"""
credentials_path = os.path.expanduser("/app/service-account-key.json")
credentials_path = os.path.expanduser("/app/account-service-key.json")

if not os.path.exists(credentials_path):
raise FileNotFoundError(f"Credentials file not found at {credentials_path}")

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = credentials_path
bucket_name = (
os.environ["GOOGLE_CLOUD_BUCKET_NAME"]
if os.environ["GOOGLE_CLOUD_BUCKET_NAME"] is not None
else "cleanandgreenphl"
)
bucket_name = os.getenv("GOOGLE_CLOUD_BUCKET_NAME", "cleanandgreenphl")

storage_client = storage.Client(project="clean-and-green-philly")
return storage_client.bucket(bucket_name)


bucket = google_cloud_bucket()


class FeatureLayer:
"""
FeatureLayer is a class to represent a GIS dataset. It can be initialized with a URL to an Esri Feature Service, a SQL query to Carto, or a GeoDataFrame.
Expand Down
25 changes: 25 additions & 0 deletions data/src/data_utils/contig_neighbors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import warnings

import geopandas as gpd
import networkx as nx
from libpysal.weights import Queen


def contig_neighbors(primary_featurelayer):

parcels = primary_featurelayer.gdf

with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=FutureWarning)
warnings.filterwarnings("ignore", category=UserWarning, message="The weights matrix is not fully connected")

w = Queen.from_dataframe(parcels)

g = w.to_networkx()

# Calculate the number of contiguous neighbors for each feature in parcels
n_contiguous = [len(nx.node_connected_component(g, i)) for i in range(len(parcels))]

primary_featurelayer.gdf['n_contiguous'] = n_contiguous

return primary_featurelayer
2 changes: 2 additions & 0 deletions data/src/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from data_utils.city_owned_properties import city_owned_properties
from data_utils.community_gardens import community_gardens
from data_utils.conservatorship import conservatorship
from data_utils.contig_neighbors import contig_neighbors
from data_utils.deliquencies import deliquencies
from data_utils.dev_probability import dev_probability
from data_utils.drug_crimes import drug_crimes
Expand Down Expand Up @@ -52,6 +53,7 @@
community_gardens,
park_priority,
ppr_properties,
contig_neighbors,
dev_probability,
]

Expand Down

0 comments on commit d09fda3

Please sign in to comment.