Here is a contour map for which all the polygons of levels are available.
Let ask how to smooth the polygons keeping all vertices preserved in their exact locations?
Indeed the contour is made on top of a grid data, you may suggest then to smooth the grid data and hence the resulting contour will be smoother. Note that this is not working as my desire since the smoothing function such as Gaussian filter will remove small packs of data and will change the range of the third variable e.g., the height which are not allowed in my application.
Actually I'm looking for a piece of code (preferably in Python) which can do the smoothing of 2D polygons (any type: convex, concave, self-intersecting etc) reasonably painless (forget pages of codes) and accurate.
FYI, there is a function in ArcGIS that does this perfectly, but using third party commercial applications is not my choice for this question.
1)
Scipy.interpolate:
As you see the resulting splines (red) are not satisfactory!
2)
Here is the result using the code given in here. It is not working well!
3)
To me the best solution should be something like the following figure in which a square is being smoothed gradually by changing only one value. I hope for similar concept for smoothing any form of polygons.
Satisfying the condition that spline passes the points:
4)
Here is my implementation of "whuber's idea" line by line in Python on his data. There are possibly some bugs since the results are not good.
K=2 is a disaster and so for k>=4.
5)
I removed one point in the problematic location and the resulting spline is now identical to whuber's. But it is still a question that why the method does not work for all cases?
6)
A good smoothing for whuber's data can be as follows (drawn by vector graphics software) in which an extra point has been smoothly added (compare with update
4):
7)
See the result from Python version of whuber's code for some iconic shapes:
Note that the method seems doesn't work for polylines. For the corner polyline (contour) green is what I want but got red. This needs to be addressed since contour maps are always polylines although closed polylines can be treated as polygons as in my examples. Also not that the problem emerged in update 4 has not been addressed yet.
8) [my last]
Here is the final solution (not perfect!):
Remember that you will have to do something about the area pointed by stars. There is perhaps a bug in my code or the proposed method needs further development to consider all situations and to provide desired outputs.