Last active
February 13, 2019 08:24
-
-
Save willu47/69d6899fd17a743c040c8d9e339334d2 to your computer and use it in GitHub Desktop.
Python script to move a city (or any shapefile) around. I couldn't work out how to detemine source lat lon from the shapefile (e.g. from centroid of convex hull of all the polygons).
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
from shapely.affinity import translate | |
from shapely.geometry import Point, MultiPolygon | |
from shapely.ops import transform | |
from functools import partial | |
import pyproj | |
import geopandas as gpd | |
import os | |
def move_city(filepath, source_latlon, destination_latlon): | |
data = gpd.read_file(filepath) | |
project = partial( | |
pyproj.transform, | |
pyproj.Proj(init='epsg:4326'), | |
pyproj.Proj(init='epsg:27700')) | |
origin = transform(project, Point(source_latlon[1], source_latlon[0])) | |
destination = transform(project, Point(destination_latlon[1], destination_latlon[0])) | |
offset_x = destination.x - origin.x | |
offset_y = destination.y - origin.y | |
data['geometry'] = data['geometry'].translate(xoff=offset_x, yoff=offset_y) | |
out = os.path.join("translated", os.path.basename(filepath)) | |
data.to_file(out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment