Reputation: 183
I have a basic shape file of all the us states which can be found here..
I am looking to edit the positions of the 2 states hawaii and alaska, i wish to change the coordinates of the state of hawaii so that it roughly sits under the state of nevada, and i also with to change the state of alaska so that is is considerably smaller.. and also so it sits roughly below both the state of california and Arizona, il include an image just so theres a visual of my idea..
as you can see alaska and hawaii are sitting on the bottom left of the large us mainland just under the states mentioned before.
I know for this to happen i need to change the longitude and latitude coordinates of both states using geopandas etc.
So i started off with the state of hawaii and began accessing the polygons coordinates using numpy. here is a snippet of the code so far
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point
import matplotlib.pyplot as plt
from shapely.geometry import Polygon
from shapely.geometry import Point, Polygon
import numpy as np
poly_States = gpd.read_file("states.shp")
hawaii = poly_States[poly_States.STATE_ABBR == "HI"]
coords = [i for i in hawaii.geometry]
all_Coords = []
for b in coords[0].boundary:
coords = np.dstack(b.coords.xy).tolist()
all_Coords.append(*coords)
for cord_1 in all_Coords:
for cord2 in cord_1:
cord2[0] = cord2[0] + 54.00000000000000
my idea here was to access the coordinates in array format and change the latitude coordinates by adding 54, so basically shifting the entire state to the right to have it sitting rougly under new mexico.
my issue lies in actually returning theses changes to the polygon object in the shapefile itself. i feel like there is probably an easier method maybe by accessing attributes of the polygon or using some sort of external software, but i believe that if im able to properly access the long,lat values and change them i should be able to make the changes in positioning and size that i need.
Thanks in advance.
Upvotes: 2
Views: 2033
Reputation: 30579
You can use translate
and assign the new geometry like this:
m = poly_States.STATE_ABBR == "HI"
poly_States[m] = poly_States[m].set_geometry(poly_States[m].translate(54))
The same way you can scale and shift Alaska:
m = poly_States.STATE_ABBR == "AK"
poly_States[m] = poly_States[m].set_geometry(poly_States[m].scale(.2,.2,.2).translate(40, -40))
Upvotes: 7