Jump to content

Python scripts in Spotfire producing different results than in Jupyter Notebooks


Jon Orth 2
Go to solution Solved by Gaia Paolini,

Recommended Posts

Good morning,

I'm fighting a problem with the shapely polygon function in a python script generating invalid geometry polygon objects from a list of coordinate points within Spotfire.  The attached file has a sample data set of X,Y coordinates and a script that creates a Voronoi tesselation, sorts and QC's the results and then generates polygon objects.  This same code works correctly in Jupyter Notebooks.  I've gone as far as taking the Python packages from the directory where Jupyter calls them and moving them into the Spotfire location so I don't think it's package related.  The issue with the polygons generated is that every other one seems to swap the x and y coordinates in the extents (XMin, XMax, YMin, YMax) while the XCenter and YCenter stay correct.  So alternating polygons generated are valid or not valid.  I'm at a loss for what to do except take this function outside of Spotfire but would appreciate any other suggestions.

 

Best regards,

Jon Orth.

SubseaVoronoiPortalOption.dxp

Link to comment
Share on other sites

  • Solution

The interpretation of a geopandas dataframe that is output into Spotfire from a Python data function is currently under review in Spotfire.
For the time being, I suggest to cast your output geopandas dataframe into a simple pandas dataframe, and do all the final data preparation (calculate bounds, centroid, turn geometry into WKB, and add metadata) within your Python script.


I have added these lines at the end of your code, generating a second output dataframe called PolyOutput2, then plotted it in the attached screenshot.

I am not sure if this is the intended result, but it looks a bit better than what you are getting now and the bounds and centroid look consistent with your coordinates.

 

# Calculate bounds and centroid manually
shape_bounds=  PolyOutput.bounds
shape_bounds = shape_bounds[['minx','maxx','miny','maxy']]
shape_bounds.columns=['XMin','XMax','YMin','YMax']
        
shape_centroid_points = PolyOutput.centroid
shape_centroid=pd.DataFrame({'XCenter':[],'YCenter':[]})
shape_centroid['XCenter'] = shape_centroid_points.map(lambda p: p.x)
shape_centroid['YCenter'] = shape_centroid_points.map(lambda p: p.y)   

# Translate geometry to WKB
wkb_geometry = gpd.GeoSeries.to_wkb(PolyOutput['geometry'])

# Downgrade to simple pandas data frame
PolyOutput2 = pd.DataFrame({'geometry':wkb_geometry})
PolyOutput2 = pd.concat([PolyOutput2,shape_bounds,shape_centroid],axis=1)

# Geocode for Spotfire (should be the last statements before output)
PolyOutput2['geometry'].spotfire_column_metadata = {"MapChart.ColumnTypeId": ["Geometry"], 
                                                "ContentType": ["application/x-wkb"]}
PolyOutput2['XMax'].spotfire_column_metadata = {"MapChart.ColumnTypeId": ["XMax"]}
PolyOutput2['YMax'].spotfire_column_metadata = {"MapChart.ColumnTypeId": ["YMax"]}
PolyOutput2['XMin'].spotfire_column_metadata = {"MapChart.ColumnTypeId": ["XMin"]}
PolyOutput2['YMin'].spotfire_column_metadata = {"MapChart.ColumnTypeId": ["YMin"]}
PolyOutput2['XCenter'].spotfire_column_metadata = {"MapChart.ColumnTypeId": ["XCenter"]}
PolyOutput2['YCenter'].spotfire_column_metadata = {"MapChart.ColumnTypeId": ["YCenter"]}

image.thumb.png.12fccf3341c4de251e04f000008da3a2.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...