0
$\begingroup$

Code used:

import matplotlib.pyplot as plt
import networkx as nx
import osmnx as ox

area = ox.geocode_to_gdf("Trafaria, Portugal")

tags_substation = {'power': 'substation'}
substation = ox.geometries_from_place("Trafaria, Portugal", tags_substation)
substation.plot()
print("Retrieved", len(substation), "substation objects")

tags_power_tower = {'power': 'tower'}
power_tower = ox.geometries_from_place("Trafaria, Portugal", tags_power_tower)
power_tower.plot()
print("Retrieved", len(power_tower), "power_tower objects")

tags_power_line = {'power': 'line'}
power_line = ox.geometries_from_place("Trafaria, Portugal", tags_power_line)
power_line.plot()
print("Retrieved", len(power_line), "power_line objects")

fig, ax = plt.subplots(figsize = (12,8))
area.plot(ax=ax, facecolor = "black")
substation.plot(ax=ax, facecolor = "red", alpha = 1)
power_tower.plot(ax=ax, facecolor = "dimgray", alpha = 1, markersize = 10)
power_line.plot(ax=ax, facecolor = "green", alpha = 0.7)

Image below is when i have only the substations, power towers and the background.

enter image description here

This image are the power lines.

enter image description here

And this one is when i have everything together like the code i developed above.

enter image description here

I dont want this to happen, i want the power lines present but only inside "area" with the same escale as the first image, if it's out of that area/background i dont want to see it. How can i do that? I'm dealing (substation, power_tower,etc) with Dataframes variables.

$\endgroup$
2
  • $\begingroup$ Simple solution would be to crop the plot (set x_min x_max, y_min, y_max for the plot, maybe with some margin) $\endgroup$
    – lcrmorin
    Feb 22 at 10:47
  • $\begingroup$ More complex solution would be to find the overlap between the polygons. $\endgroup$
    – lcrmorin
    Feb 22 at 10:53

1 Answer 1

2
$\begingroup$

To plot only the power lines that are within the area and not outside of it, you can use the geopandas library to perform a spatial join between the power_line dataframe and the area dataframe. This will return only the power lines that are within the area.

Here is the modified code that should achieve what you're looking for:

import matplotlib.pyplot as plt
import networkx as nx
import osmnx as ox
import geopandas as gpd

area = ox.geocode_to_gdf("Trafaria, Portugal")

tags_substation = {'power': 'substation'}
substation = ox.geometries_from_place("Trafaria, Portugal", tags_substation)
substation.plot()
print("Retrieved", len(substation), "substation objects")

tags_power_tower = {'power': 'tower'}
power_tower = ox.geometries_from_place("Trafaria, Portugal", tags_power_tower)
power_tower.plot()
print("Retrieved", len(power_tower), "power_tower objects")

tags_power_line = {'power': 'line'}
power_line = ox.geometries_from_place("Trafaria, Portugal", tags_power_line)

# perform a spatial join to keep only power lines that are within the area
power_line = gpd.sjoin(power_line, area, op='intersects')
power_line.plot()

print("Retrieved", len(power_line), "power_line objects within the area")

fig, ax = plt.subplots(figsize=(12,8))
area.plot(ax=ax, facecolor="black")
substation.plot(ax=ax, facecolor="red", alpha=1)
power_tower.plot(ax=ax, facecolor="dimgray", alpha=1, markersize=10)
power_line.plot(ax=ax, facecolor="green", alpha=0.7)

plt.show()

```
$\endgroup$
1
  • 1
    $\begingroup$ Everything seems to be the same (except there was a need to delete 'index_left' from power_line), it didn't work for me. Perhaps because those lines are inside the area and just happened to fall out? But thanks anyway $\endgroup$
    – Scipio
    Feb 17 at 18:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.