Open Eslam-mohakim opened 4 months ago
The error message setting an array element with a sequence is generally an indication that you're trying to set an array (or in this case, a column in a DataFrame) with a list or a sequence of values when it's expecting only a single value.
A possible problem may be that Polygon(coordinates) is returning a sequence (or list) of values instead of a single value and hence the error. Also, you might want to check that each dict your function checks contains rings that themselves do not contain lists that are sequences. try this:
def create_geometry(x): if x == x and isinstance(x, dict) and "rings" in x and isinstance(x["rings"], list) and len(x["rings"]) > 0:
is a coordinate pair coordinates = [tuple(i) for i in x["rings"]]
print(coordinates)# look if the coordinates is neasted list
return Polygon(coordinates)
else:
return np.nan
don't forget to import numpy
impoert numpy as np
Il giorno mar 23 apr 2024 alle ore 09:54 Eslam-mohakim < @.***> ha scritto:
when trying to use the same code df["geometry"] = df["area"].apply(create_geometry) but for area for Polygon type, it fires this error: "ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (25,)
- inhomogeneous part."
this is the function: def create_geometry(x): if x == x and isinstance(x, dict) and "rings" in x and isinstance(x["rings"], list) and len(x["rings"]) > 0: return Polygon(coordinates) else: return np.nan
— Reply to this email directly, view it on GitHub https://github.com/enzococca/Survey123ToLayer/issues/1, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA2P5KEJPPBFB6TSO6RARKLY6YHUDAVCNFSM6AAAAABGUKXMZSVHI2DSMVQWIX3LMV43ASLTON2WKOZSGI2TQMRQGU2DAMI . You are receiving this because you are subscribed to this thread.Message ID: @.***>
-- Enzo Cocca PhD in "Science and Technology for Archaeology and Cultural Heritage"
ISMEO International Association of Mediterranean and Oriental Studies
mail: @.*** cell: +393495087014
Furthermore, you should then apply the create_geometry function instead of:
df["geometry"] = df["location"].apply( lambda x: Polygon(x["rings"][0]) ) in
df["geometry"] = df["area"].apply(create_geometry) )
when trying to use the same code
df["geometry"] = df["area"].apply(create_geometry)
but for area for Polygon type, it fires this error: "ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (25,) + inhomogeneous part."this is the function:
def create_geometry(x): if x == x and isinstance(x, dict) and "rings" in x and isinstance(x["rings"], list) and len(x["rings"]) > 0: return Polygon(coordinates) else: return np.nan