shapely's scale function in geopandas returns points of similar magnitude

4.6k views Asked by At

I have a .shp file that I read into a geopandas dataframe. I change the coordinate reference system to 2163 since I'm making some rectangular maps and want them to look somewhat normal.

geo_df = geo.GeoDataFrame.from_file('path to shp files here')
geo_df = geo_df.to_crs(epsg=2163)

Dataframe looks like this:

In [10]: geo_df.geometry
Out[10]: 
0       POLYGON ((1189879.121395004 -1019103.847184072...
1       POLYGON ((1220434.289428635 -1303875.122589418...
2       POLYGON ((1210969.088787247 -1295221.772496042...
3       POLYGON ((1217371.162725744 -1300978.843646188...

Now I'm computing a pairwise distance matrix, but since these coordinates are given in meters, the numbers are quite large and I run into overflow problems. So I try this:

geo_df.geometry = geo_df.geometry.scale(xfact=1/10000, yfact=1/10000, zfact=1.0)

But the coordinates of my polygons are not any smaller (just shifted a bit, which is fine). Printing the supposedly rescaled dataframe gives this:

In [12]: geo_df.geometry.scale(xfact=1/10000, yfact=1/10000, zfact=1.0)
Out[12]: 
0       POLYGON ((1197230.99656014 -1026296.564074459,...
1       POLYGON ((1221475.564597901 -1304490.752661498...
2       POLYGON ((1215982.083961291 -1290526.930856638...
3       POLYGON ((1223839.012413891 -1292585.80012823,...

I was hoping to get something like this.

Out[12]: 
0       POLYGON ((119.723099656014 -102.6296564074459,...
1       POLYGON ((122.1475564597901 -130.4490752661498...
2       POLYGON ((121.5982083961291 -129.0526930856638...
3       POLYGON ((122.3839012413891 -129.258580012823,...

Maybe I just don't understand what shapely's scaling function is supposed to do, though it seems pretty straightforward.

1

There are 1 answers

0
jdmcbr On BEST ANSWER

The scaling is defined relative to a center point. The scaling you applied actually did make the polygons smaller, but kept the center point at the center of the bounding box of the polygon. The origin keyword argument in shapely.affinity.scale defaults to the polygon center; to reduce the coordinate values by a factor of 10000, you can set origin=(0, 0), as follows:

geo_df.geometry = geo_df.geometry.scale(xfact=1/10000, yfact=1/10000, zfact=1.0, origin=(0, 0))