I have raster data where each pixel is assigned a feature id and the features form connected pixel regions. We could think of this as a rasterized map of different countries.
raster = np.array([
[0, 1, 1, 1, 2, 2],
[0, 1, 1, 2, 2, 3],
[0, 1, 4, 4, 4, 3],
[0, 0, 0, 0, 0, 0]
])
This can be turned into a GeoPandas GeoDataFrame using rasterio.features.shapes:
geo_json_shapes, feature_ids = zip(*rio.features.shapes(raster, connectivity=4))
shapes = [shapely.geometry.shape(g) for g in geo_json_shapes]
gdf = gpd.GeoDataFrame(dict(id=feature_ids, geometry=shapes))
gdf.plot(column='id', edgecolor='red')
What I would like to do from this starting point is basically the inverse of geopandas.GeoSeries.polygonize, which takes a series of LineStrings and puts them together to form Polygons. That is, I want to extract from this a list of borders between countries. For example the border between countries with ids 1 and 2 would be a LineString with coordinates (4,0), (4,1), (3,1), (3,2). Hence, counting pixels outside the raster as sixth region with id None, this would results in a series of 12 LineStrings, each of them equipped with two ids for the two countries it is the common boundary of.
Is there any function of GeoPandas or similar that can be used for this?
In the end the goal is to simplify these borders by applying some line generalisation algorithm (with parameters depending on which feature ids are involved) to the individual LineStrings and then put everything back together into a GeoDataFrame of simplified features.

