Skip to content

Instantly share code, notes, and snippets.

@asdrubalivan
Created April 23, 2023 20:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asdrubalivan/d1a7b7b5beec671f4c11f2e65487f305 to your computer and use it in GitHub Desktop.
Save asdrubalivan/d1a7b7b5beec671f4c11f2e65487f305 to your computer and use it in GitHub Desktop.
Traslado de estados
import geopandas as gpd
import folium
from shapely.geometry import mapping, Polygon, Point
from shapely.affinity import translate
from shapely.ops import nearest_points
# Read in the shapefile as a GeoDataFrame
PATH = 'PATH' # CAN BE DOWNLOADED https://www.efrainmaps.es/descargas-gratuitas/venezuela/capas-base/
ESTADO = "Trujillo"
gdf = gpd.read_file(PATH)
trujillo_poly = gdf[gdf['ESTADO'] == ESTADO].geometry.iloc[0]
# Define the target coordinate (in decimal degrees)
target_coord = (51, 5.0)
# Find the nearest point on the polygon to the target coordinate
nearest_point, _ = nearest_points(trujillo_poly, Point(target_coord))
# Calculate the distance between the current location and the target location
x_offset = target_coord[1] - nearest_point.x
y_offset = target_coord[0] - nearest_point.y
# Translate the polygon to a new location
trujillo_poly_translated = translate(trujillo_poly, xoff=x_offset, yoff=y_offset)
# Replace the original Trujillo polygon with the translated one
gdf.loc[gdf['ESTADO'] == ESTADO, 'geometry'] = trujillo_poly_translated
# Create a folium map centered on New York
m = folium.Map(location=[40.7128, -74.0060], zoom_start=4)
# Convert the GeoDataFrame to GeoJSON format
geojson_data = gdf.to_crs(epsg='4326').to_json()
# Add the GeoJSON data to the folium map as a choropleth layer
folium.Choropleth(
geo_data=geojson_data,
fill_color='YlGn',
fill_opacity=0.7,
line_opacity=0.2,
name='My Shapefile'
).add_to(m)
# Add a layer control panel to the map
folium.LayerControl().add_to(m)
m.save("my_map.html")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment