1919#
2020
2121import pyrasterframes .rf_types
22+ from shapely .geometry .base import BaseGeometry
23+
2224import numpy as np
2325
2426
@@ -120,13 +122,18 @@ def pandas_df_to_html(df):
120122 if not pd .get_option ("display.notebook_repr_html" ):
121123 return None
122124
125+ default_max_colwidth = pd .get_option ('display.max_colwidth' ) # we'll try to politely put it back
126+
123127 if len (df ) == 0 :
124128 return df ._repr_html_ ()
125129
126130 tile_cols = []
131+ geom_cols = []
127132 for c in df .columns :
128133 if isinstance (df .iloc [0 ][c ], pyrasterframes .rf_types .Tile ): # if the first is a Tile try formatting
129134 tile_cols .append (c )
135+ elif isinstance (df .iloc [0 ][c ], BaseGeometry ): # if the first is a Geometry try formatting
136+ geom_cols .append (c )
130137
131138 def _safe_tile_to_html (t ):
132139 if isinstance (t , pyrasterframes .rf_types .Tile ):
@@ -135,11 +142,21 @@ def _safe_tile_to_html(t):
135142 # handles case where objects in a column are not all Tile type
136143 return t .__repr__ ()
137144
145+ def _safe_geom_to_html (g ):
146+ if isinstance (g , BaseGeometry ):
147+ wkt = g .wkt
148+ if len (wkt ) > default_max_colwidth :
149+ return wkt [:default_max_colwidth - 3 ] + '...'
150+ else :
151+ wkt
152+ else :
153+ return g .__repr__ ()
154+
138155 # dict keyed by column with custom rendering function
139156 formatter = {c : _safe_tile_to_html for c in tile_cols }
157+ formatter .update ({c : _safe_geom_to_html for c in geom_cols })
140158
141159 # This is needed to avoid our tile being rendered as `<img src="only up to fifty char...`
142- default_max_colwidth = pd .get_option ('display.max_colwidth' ) # we'll try to politely put it back
143160 pd .set_option ('display.max_colwidth' , - 1 )
144161 return_html = df .to_html (escape = False , # means our `< img` does not get changed to `< img`
145162 formatters = formatter , # apply custom format to columns
0 commit comments