2

I'm trying to implement a transformation of x and y coordinates for line features of a Shapefile. geopandas.set_geometry() isn't setting the x and y coordinates and isn't giving me any errors.

Here is my code:

import shapely
import geopandas as gpd

def calcNewXY(geom):
    return geom.geometry.map(
        lambda line: shapely.ops.transform(lambda x, y: (x - 100000, y - 10000), line)
    )

data = gpd.read_file(inFile)
data.set_geometry(calcNewXY(data))

The actual coordinate transformation works, I debugged this part. Maybe somebody can shed some light on what I'm missing here.

SOLUTION:

inplace=True has to be set as suggested by @mikewatt

data.set_geometry(calcNewXY(data), inplace=True)
10
  • just an error because i simplified the x and y calculation. this part acutally works. i checked that.
    – zwnk
    Jan 4 at 21:07
  • 2
    Can you provide some data sample for reproducibility? Jan 4 at 21:15
  • 2
    shapely.ops.transform takes two args, func and geom. You're only feeding in func as written, it should be throwing a syntax error. If I were you I'd re-write this using named functions to make it easier to debug, nested lambdas is a lil messy
    – mikewatt
    Jan 4 at 21:22
  • 2
    Got it. Works now. Looks like you need data.set_geometry(calcNewXY(data), inplace=True) to avoid creating a new object: geopandas.org/en/stable/docs/reference/api/…
    – mikewatt
    Jan 4 at 21:50
  • 2
    Please put your edit as a valid answer!
    – Taras
    Jan 4 at 22:07

1 Answer 1

4

Another possible solution is to overwrite the data i.e.

data = data.set_geometry(calcNewXY(data))

See GeoPandas documentation for more details.

1
  • 2
    This is preferable to operating in-place and it opens the possibility to chain additional operations
    – Paul H
    Jan 4 at 22:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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