-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
412 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import geopandas as gpd | ||
import shapely.geometry as geo | ||
import numpy as np | ||
import pandas as pd | ||
from tqdm import tqdm | ||
import pickle | ||
|
||
center = geo.Point(651791.0, 6862293.0) | ||
radius = 10000 | ||
|
||
df_iris = gpd.read_file("../data/CONTOURS-IRIS.shp") | ||
df_iris = df_iris[df_iris.geometry.centroid.distance(center) < radius] | ||
|
||
df_iris = df_iris[["CODE_IRIS", "geometry"]].rename( | ||
columns = { "CODE_IRIS": "iris_id" } | ||
) | ||
|
||
df_municipality = df_iris.copy() | ||
df_municipality["municipality_id"] = df_municipality["iris_id"].str[:-4] | ||
del df_municipality["iris_id"] | ||
df_municipality = df_municipality.dissolve("municipality_id").reset_index() | ||
|
||
df_trips = pd.read_csv("../data/trips_alpha1.0.csv", sep = ";") | ||
|
||
df_trips["origin_geometry"] = [ | ||
geo.Point(*p) for p in tqdm(zip(df_trips["origin_x"], df_trips["origin_y"]), total = len(df_trips)) | ||
] | ||
|
||
df_trips["destination_geometry"] = [ | ||
geo.Point(*p) for p in tqdm(zip(df_trips["destination_x"], df_trips["destination_y"]), total = len(df_trips)) | ||
] | ||
|
||
df_trips = gpd.GeoDataFrame(df_trips, geometry = "origin_geometry") | ||
|
||
print("Filtering origin geometry") | ||
df_trips = df_trips.set_geometry("origin_geometry") | ||
df_trips = df_trips[df_trips.centroid.distance(center) < radius] | ||
|
||
print("Filtering destination geometry") | ||
df_trips = df_trips.set_geometry("destination_geometry") | ||
df_trips = df_trips[df_trips.centroid.distance(center) < radius] | ||
|
||
print("Joining origin municipality") | ||
df_trips = df_trips.set_geometry("origin_geometry") | ||
df_trips = gpd.sjoin(df_trips, df_municipality, op = "within").drop(["index_right"], axis = 1) | ||
df_trips = df_trips.rename(columns = { "municipality_id": "origin_municipality_id" }) | ||
|
||
print("Joining destination municipality") | ||
df_trips = df_trips.set_geometry("destination_geometry") | ||
df_trips = gpd.sjoin(df_trips, df_municipality, op = "within").drop(["index_right"], axis = 1) | ||
df_trips = df_trips.rename(columns = { "municipality_id": "destination_municipality_id" }) | ||
|
||
with open("../data/trips.p", "wb+") as f: | ||
pickle.dump(df_trips, f) | ||
|
||
print(df_trips) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<template> | ||
<g class="layer"> | ||
<path | ||
v-for="zone in zones" | ||
v-bind:key="zone.id" | ||
v-bind:d="zone.geometry" | ||
v-on:mouseenter="onEnter(zone)" | ||
v-on:mouseleave="onLeave(zone)" | ||
v-on:click="onClick(zone)" | ||
v-bind:style="{ fill: zone.color }" | ||
/> | ||
</g> | ||
</template> | ||
|
||
<script> | ||
import * as d3 from "d3"; | ||
export default { | ||
name: "ZoneLayer", | ||
data() { | ||
return { | ||
hoverZone: undefined, clickZone: undefined, | ||
width: 0, height: 0, | ||
hoverSelectedMunicipalityId: undefined, | ||
clickSelectedMunicipalityId: undefined | ||
}; | ||
}, | ||
props: ["layerState"], | ||
computed: { | ||
zones() { | ||
var scale = this.layerState.scale; | ||
var width = this.width; | ||
var height = this.height; | ||
var offset = this.layerState.offset; | ||
var projection = d3.geoTransform({ | ||
point: function(x, y) { | ||
this.stream.point( | ||
(x - 651791.0) * scale + width * 0.5 + offset[0], | ||
(y - 6862293.0) * scale + height * 0.5 + offset[1] | ||
); | ||
} | ||
}); | ||
var generator = d3.geoPath() | ||
.projection(projection); | ||
var baseColor = d3.color("steelblue"); | ||
var selectedMunicipalityId = this.hoverSelectedMunicipalityId; | ||
if (selectedMunicipalityId == undefined) { | ||
selectedMunicipalityId = this.clickSelectedMunicipalityId; | ||
} | ||
return this.layerState.features.map((item) => { | ||
var color = d3.color(baseColor); | ||
color.opacity = 0.0; | ||
if (selectedMunicipalityId != undefined) { | ||
var selectedValues = this.layerState.data[selectedMunicipalityId]; | ||
if (selectedValues != undefined) { | ||
var value = selectedValues[item.properties.municipality_id]; | ||
if (value != undefined) { | ||
color.opacity = (value - this.layerState.minimumValue) / (this.layerState.maximumValue - this.layerState.minimumValue) | ||
} | ||
} | ||
} | ||
/*color.opacity = (item.properties.value - this.layerState.minimumValue) / (this.layerState.maximumValue - this.layerState.minimumValue) | ||
color.opacity = 0.0;*/ | ||
return { id: item.properties.municipality_id, color: color, geometry: generator(item) }; | ||
}); | ||
} | ||
}, | ||
methods: { | ||
onClick(zone) { | ||
if (this.clickedElement != undefined) { | ||
this.clickedElement.classList.remove("selected"); | ||
} | ||
this.clickSelectedMunicipalityId = zone.id; | ||
this.clickedElement = event.target; | ||
this.clickedElement.classList.add("selected"); | ||
}, | ||
onEnter(zone) { | ||
this.hoverSelectedMunicipalityId = zone.id; | ||
this.hoverZone = zone; | ||
event.target.parentNode.appendChild(event.target); | ||
event.target.classList.add("hover") | ||
this.$emit("hoverZone", zone); | ||
if (this.clickedElement != undefined) { | ||
this.clickedElement.parentNode.appendChild(this.clickedElement); | ||
} | ||
}, | ||
onLeave() { | ||
this.hoverSelectedMunicipalityId = undefined; | ||
this.hoverZone = undefined; | ||
event.target.classList.remove("hover") | ||
this.$emit("hoverZone", undefined); | ||
}, | ||
onResize() { | ||
this.width = this.$el.parentNode.clientWidth; | ||
this.height = this.$el.parentNode.clientHeight; | ||
} | ||
}, | ||
mounted() { | ||
this.onResize(); | ||
window.addEventListener('resize', this.onResize); | ||
} | ||
} | ||
</script> | ||
|
||
<style scoped> | ||
path { | ||
stroke: #cccccc; | ||
fill: white; | ||
} | ||
path.selected { | ||
stroke: blue; | ||
stroke-width: 2; | ||
} | ||
path.hover { | ||
stroke: red; | ||
} | ||
</style> |
Oops, something went wrong.