-
-
Save houssemFat-DDF/56e5ed53056f0d3a2f6a43488c4fe845 to your computer and use it in GitHub Desktop.
Read a PNG into a numpy array, convert it to a Shapely Polygon, and dump it as GeoJSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| """ | |
| Created by Stephan Hügel on 2017-03-02 | |
| The MIT License (MIT) | |
| Copyright (c) 2017 Stephan Hügel | |
| Permission is hereby granted, free of charge, to any person obtaining a copy | |
| of this software and associated documentation files (the "Software"), to deal | |
| in the Software without restriction, including without limitation the rights | |
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| copies of the Software, and to permit persons to whom the Software is | |
| furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in | |
| all copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| THE SOFTWARE. | |
| """ | |
| # requires scipy, scikit-image, shapely, geojson and dependencies | |
| # this script is simple, but "heavy" on packages bc scipy and scikit | |
| from scipy import misc | |
| from skimage import color, measure | |
| from shapely.geometry import shape, Point, Polygon, LineString | |
| import geojson | |
| # read a PNG | |
| polypic = misc.imread('poly.png') | |
| # convert to greyscale if need be | |
| gray = color.colorconv.rgb2grey(polypic) | |
| # find contours | |
| # Not sure why 1.0 works as a level -- maybe experiment with lower values | |
| contours = measure.find_contours(gray, 1.0) | |
| # build polygon, and simplify its vertices if need be | |
| # this assumes a single, contiguous shape | |
| # if you have e.g. multiple shapes, build a MultiPolygon with a list comp | |
| # RESULTING POLYGONS ARE NOT GUARANTEED TO BE SIMPLE OR VALID | |
| # check this yourself using e.g. poly.is_valid | |
| poly = Polygon(contours[0]).simplify(1.0) | |
| # write out to cwd as JSON | |
| with open('polygon.json', 'w') as f: | |
| f.write(geojson.dumps(poly)) |
This file has been truncated, but you can view the full file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
