Skip to content

Instantly share code, notes, and snippets.

@sgillies
Last active April 24, 2021 21:57
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgillies/8655640 to your computer and use it in GitHub Desktop.
Save sgillies/8655640 to your computer and use it in GitHub Desktop.
Rasterio example
Display the source blob
Display the rendered blob
Raw
{"type": "FeatureCollection", "features": [{"geometry": {"type": "Polygon", "coordinates": [[[0.5, 37.5], [0.5, 37.0], [0.0, 37.0], [-0.5, 37.0], [-0.5, 21.0], [1.5, 21.0], [1.5, 28.5], [2.0, 28.5], [2.0, 29.0], [2.5, 29.0], [2.5, 29.5], [3.5, 29.5], [4.0, 29.5], [4.0, 30.0], [4.5, 30.0], [4.5, 29.5], [5.5, 29.5], [5.5, 28.5], [6.0, 28.5], [6.0, 21.0], [7.5, 21.0], [8.0, 21.0], [8.0, 30.0], [7.5, 30.0], [7.5, 30.5], [7.0, 30.5], [7.0, 31.0], [6.5, 31.0], [6.5, 31.5], [3.5, 31.5], [3.5, 31.0], [3.0, 31.0], [2.0, 31.0], [2.0, 30.5], [1.5, 30.5], [1.5, 37.5], [0.5, 37.5]]]}, "type": "Feature", "properties": {"raster_val": 0}}, {"geometry": {"type": "Polygon", "coordinates": [[[-4.5, 35.0], [-4.5, 34.5], [-5.0, 34.5], [-6.0, 34.5], [-6.0, 31.5], [-6.5, 31.5], [-7.0, 31.5], [-7.0, 30.0], [-6.0, 30.0], [-6.0, 22.0], [-5.5, 22.0], [-5.5, 21.5], [-5.0, 21.5], [-5.0, 21.0], [-4.0, 21.0], [-4.0, 20.5], [-2.0, 20.5], [-2.0, 22.0], [-3.0, 22.0], [-3.0, 22.5], [-3.5, 22.5], [-3.5, 23.0], [-4.0, 23.0], [-4.0, 30.0], [-2.5, 30.0], [-2.0, 30.0], [-2.0, 31.5], [-4.0, 31.5], [-4.0, 35.0], [-4.5, 35.0]]]}, "type": "Feature", "properties": {"raster_val": 0}}, {"geometry": {"type": "Polygon", "coordinates": [[[22.5, 31.5], [22.5, 31.0], [22.0, 31.0], [21.5, 31.0], [21.5, 30.5], [21.0, 30.5], [20.5, 30.5], [20.5, 30.0], [20.0, 30.0], [20.0, 21.0], [22.5, 21.0], [22.5, 29.5], [23.0, 29.5], [23.0, 30.0], [25.5, 30.0], [25.5, 29.5], [26.0, 29.5], [26.0, 29.0], [26.5, 29.0], [26.5, 21.0], [28.0, 21.0], [28.5, 21.0], [28.5, 30.5], [28.0, 30.5], [28.0, 31.0], [27.0, 31.0], [27.0, 31.5], [22.5, 31.5]]]}, "type": "Feature", "properties": {"raster_val": 0}}, {"geometry": {"type": "Polygon", "coordinates": [[[12.0, 31.5], [12.0, 31.0], [11.5, 31.0], [11.5, 30.5], [11.0, 30.5], [10.5, 30.5], [10.5, 29.5], [10.0, 29.5], [10.0, 28.5], [9.5, 28.5], [9.5, 23.5], [10.0, 23.5], [10.0, 22.5], [10.5, 22.5], [10.5, 22.0], [11.0, 22.0], [11.0, 21.5], [11.5, 21.5], [11.5, 21.0], [13.5, 21.0], [13.5, 20.5], [15.0, 20.5], [15.0, 21.0], [16.0, 21.0], [16.5, 21.0], [16.5, 21.5], [17.0, 21.5], [17.0, 22.0], [17.5, 22.0], [18.0, 22.0], [18.0, 23.0], [18.5, 23.0], [18.5, 24.0], [19.0, 24.0], [19.0, 28.5], [18.5, 28.5], [18.5, 29.5], [18.0, 29.5], [18.0, 30.0], [17.5, 30.0], [17.5, 30.5], [17.0, 30.5], [17.0, 31.0], [16.5, 31.0], [16.5, 31.5], [12.0, 31.5]], [[15.5, 23.0], [15.5, 22.5], [15.0, 22.5], [14.5, 22.5], [14.5, 22.0], [14.0, 22.0], [14.0, 22.5], [13.0, 22.5], [13.0, 23.0], [12.5, 23.0], [12.5, 23.5], [12.0, 23.5], [12.0, 25.0], [11.5, 25.0], [11.5, 27.5], [12.0, 27.5], [12.0, 29.0], [12.5, 29.0], [12.5, 29.5], [13.0, 29.5], [13.0, 30.0], [15.0, 30.0], [15.0, 29.5], [15.5, 29.5], [15.5, 29.0], [16.0, 29.0], [16.0, 28.5], [16.5, 28.5], [16.5, 24.0], [16.0, 24.0], [16.0, 23.0], [15.5, 23.0]]]}, "type": "Feature", "properties": {"raster_val": 0}}, {"geometry": {"type": "Polygon", "coordinates": [[[-26.0, 31.5], [-26.0, 31.0], [-26.5, 31.0], [-27.0, 31.0], [-27.0, 30.5], [-27.5, 30.5], [-28.0, 30.5], [-28.0, 16.0], [-27.0, 16.0], [-27.0, 15.5], [-26.0, 15.5], [-26.0, 21.0], [-24.5, 21.0], [-24.5, 20.5], [-22.5, 20.5], [-22.5, 21.0], [-21.5, 21.0], [-21.0, 21.0], [-21.0, 21.5], [-20.5, 21.5], [-20.5, 22.0], [-20.0, 22.0], [-20.0, 22.5], [-19.5, 22.5], [-19.5, 23.5], [-19.0, 23.5], [-19.0, 28.5], [-19.5, 28.5], [-19.5, 29.5], [-20.0, 29.5], [-20.0, 30.5], [-20.5, 30.5], [-20.5, 31.0], [-21.5, 31.0], [-21.5, 31.5], [-26.0, 31.5]], [[-25.0, 30.0], [-22.5, 30.0], [-22.5, 29.5], [-22.0, 29.5], [-22.0, 28.5], [-21.5, 28.5], [-21.5, 24.0], [-22.0, 24.0], [-22.0, 23.0], [-22.5, 23.0], [-22.5, 22.5], [-25.5, 22.5], [-25.5, 23.0], [-26.0, 23.0], [-26.0, 29.5], [-25.5, 29.5], [-25.0, 29.5], [-25.0, 30.0]]]}, "type": "Feature", "properties": {"raster_val": 0}}, {"geometry": {"type": "Polygon", "coordinates": [[[-17.0, 31.5], [-17.0, 31.0], [-17.5, 31.0], [-17.5, 22.5], [-17.0, 22.5], [-17.0, 21.5], [-16.0, 21.5], [-16.0, 21.0], [-15.0, 21.0], [-15.0, 20.5], [-12.5, 20.5], [-12.5, 21.0], [-10.5, 21.0], [-10.5, 20.0], [-11.0, 20.0], [-11.0, 18.5], [-11.5, 18.5], [-11.5, 18.0], [-12.0, 18.0], [-12.0, 17.5], [-12.5, 17.5], [-14.0, 17.5], [-14.0, 17.0], [-14.5, 17.0], [-15.0, 17.0], [-15.0, 16.0], [-14.5, 16.0], [-14.5, 15.5], [-13.0, 15.5], [-13.0, 16.0], [-11.5, 16.0], [-11.0, 16.0], [-11.0, 16.5], [-10.5, 16.5], [-10.0, 16.5], [-10.0, 17.0], [-9.5, 17.0], [-9.5, 18.0], [-9.0, 18.0], [-9.0, 19.5], [-8.5, 19.5], [-8.5, 31.5], [-11.0, 31.5], [-11.0, 23.0], [-11.5, 23.0], [-11.5, 22.5], [-14.5, 22.5], [-14.5, 23.0], [-15.0, 23.0], [-15.0, 23.5], [-15.5, 23.5], [-15.5, 31.5], [-17.0, 31.5]]]}, "type": "Feature", "properties": {"raster_val": 0}}, {"geometry": {"type": "Polygon", "coordinates": [[[-4.5, 13.5], [-4.5, 13.0], [-5.0, 13.0], [-7.0, 13.0], [-7.0, 12.5], [-7.5, 12.5], [-8.0, 12.5], [-8.0, 12.0], [-8.5, 12.0], [-9.0, 12.0], [-9.0, 11.5], [-9.5, 11.5], [-9.5, 11.0], [-10.0, 11.0], [-10.0, 9.5], [-10.5, 9.5], [-10.5, 3.5], [0.0, 3.5], [0.0, 3.0], [-0.5, 3.0], [-15.0, 3.0], [-15.0, 2.5], [-15.5, 2.5], [-17.0, 2.5], [-17.0, 2.0], [-17.5, 2.0], [-18.0, 2.0], [-18.0, 1.5], [-18.5, 1.5], [-18.5, 1.0], [-19.0, 1.0], [-19.0, 0.5], [-19.5, 0.5], [-19.5, -0.5], [-20.0, -0.5], [-20.0, -1.5], [-20.5, -1.5], [-20.5, -3.5], [-21.0, -3.5], [-21.0, -12.0], [-20.5, -12.0], [-20.5, -13.5], [-20.0, -13.5], [-20.0, -15.0], [-19.5, -15.0], [-19.5, -15.5], [-19.0, -15.5], [-19.0, -16.5], [-18.5, -16.5], [-18.5, -17.0], [-17.5, -17.0], [-17.5, -17.5], [-16.5, -17.5], [-16.5, -18.0], [-11.0, -18.0], [-11.0, -11.5], [-10.5, -11.5], [-10.5, -10.5], [-10.0, -10.5], [-10.0, -9.5], [-9.5, -9.5], [-9.5, -9.0], [-9.0, -9.0], [-9.0, -8.5], [-8.5, -8.5], [-8.0, -8.5], [-8.0, -8.0], [-7.5, -8.0], [-7.5, -7.5], [-6.0, -7.5], [-5.5, -7.5], [-5.5, -7.0], [7.0, -7.0], [7.5, -7.0], [7.5, -6.5], [8.0, -6.5], [8.0, -6.0], [8.5, -6.0], [9.0, -6.0], [9.0, -5.0], [9.5, -5.0], [9.5, -4.5], [10.0, -4.5], [10.0, -2.5], [10.5, -2.5], [10.5, 8.5], [10.0, 8.5], [10.0, 10.0], [9.5, 10.0], [9.5, 11.0], [9.0, 11.0], [9.0, 11.5], [8.5, 11.5], [8.5, 12.0], [7.5, 12.0], [7.5, 12.5], [6.5, 12.5], [6.5, 13.0], [4.5, 13.0], [4.5, 13.5], [-4.5, 13.5]], [[-4.5, 7.5], [-4.5, 7.0], [-5.0, 7.0], [-5.5, 7.0], [-5.5, 6.5], [-6.0, 6.5], [-6.0, 7.0], [-7.0, 7.0], [-7.0, 8.0], [-7.5, 8.0], [-7.5, 8.5], [-7.0, 8.5], [-7.0, 9.5], [-6.5, 9.5], [-6.5, 10.0], [-5.0, 10.0], [-5.0, 9.5], [-4.5, 9.5], [-4.5, 9.0], [-4.0, 9.0], [-4.0, 7.5], [-4.5, 7.5]]]}, "type": "Feature", "properties": {"raster_val": 0}}, {"geometry": {"type": "Polygon", "coordinates": [[[11.0, 3.0], [11.0, -4.0], [10.5, -4.0], [10.5, -4.5], [10.0, -4.5], [10.0, -5.5], [9.5, -5.5], [9.5, -6.0], [9.0, -6.0], [9.0, -6.5], [8.5, -6.5], [8.5, -7.0], [8.0, -7.0], [7.5, -7.0], [7.5, -7.5], [7.0, -7.5], [6.5, -7.5], [6.5, -8.0], [6.0, -8.0], [-6.5, -8.0], [-6.5, -8.5], [-7.0, -8.5], [-7.5, -8.5], [-7.5, -9.0], [-8.0, -9.0], [-8.5, -9.0], [-8.5, -9.5], [-9.0, -9.5], [-9.0, -10.0], [-9.5, -10.0], [-9.5, -11.0], [-10.0, -11.0], [-10.0, -12.5], [-10.5, -12.5], [-10.5, -23.5], [-10.0, -23.5], [-10.0, -25.0], [-9.5, -25.0], [-9.5, -26.0], [-9.0, -26.0], [-9.0, -26.5], [-8.5, -26.5], [-8.5, -27.0], [-7.5, -27.0], [-7.5, -27.5], [-6.5, -27.5], [-6.5, -28.0], [-5.0, -28.0], [-5.0, -28.5], [-3.0, -28.5], [-3.0, -29.0], [3.0, -29.0], [3.0, -28.5], [4.5, -28.5], [5.0, -28.5], [5.0, -28.0], [6.0, -28.0], [6.5, -28.0], [6.5, -27.5], [7.0, -27.5], [7.5, -27.5], [7.5, -27.0], [8.0, -27.0], [8.5, -27.0], [8.5, -26.5], [9.0, -26.5], [9.0, -26.0], [9.5, -26.0], [9.5, -25.5], [10.0, -25.5], [10.0, -23.5], [10.5, -23.5], [10.5, -19.0], [0.0, -19.0], [0.0, -18.0], [16.5, -18.0], [17.0, -18.0], [17.0, -17.5], [17.5, -17.5], [18.0, -17.5], [18.0, -17.0], [18.5, -17.0], [18.5, -16.5], [19.0, -16.5], [19.0, -15.5], [19.5, -15.5], [19.5, -15.0], [20.0, -15.0], [20.0, -13.5], [20.5, -13.5], [20.5, -12.0], [21.0, -12.0], [21.0, -9.0], [21.5, -9.0], [21.5, -7.0], [21.0, -7.0], [21.0, -3.5], [20.5, -3.5], [20.5, -1.5], [20.0, -1.5], [20.0, 0.0], [19.5, 0.0], [19.5, 0.5], [19.0, 0.5], [19.0, 1.5], [18.5, 1.5], [18.5, 2.0], [17.5, 2.0], [17.5, 2.5], [16.0, 2.5], [16.0, 3.0], [11.0, 3.0]], [[5.0, -22.0], [5.0, -22.5], [4.5, -22.5], [4.5, -23.0], [4.0, -23.0], [4.0, -24.0], [4.5, -24.0], [4.5, -25.0], [6.5, -25.0], [6.5, -24.5], [7.0, -24.5], [7.0, -24.0], [7.5, -24.0], [7.5, -23.0], [7.0, -23.0], [7.0, -22.5], [6.5, -22.5], [6.5, -22.0], [5.0, -22.0]]]}, "type": "Feature", "properties": {"raster_val": 0}}, {"geometry": {"type": "Polygon", "coordinates": [[[-12.0, -34.0], [-12.0, -35.5], [-11.5, -35.5], [-11.5, -37.0], [-11.0, -37.0], [-11.0, -38.5], [-10.5, -38.5], [-10.5, -40.0], [-10.0, -40.0], [-10.0, -41.5], [-9.5, -41.5], [-9.5, -42.5], [-8.0, -42.5], [-8.0, -42.0], [-7.5, -42.0], [-7.5, -40.5], [-7.0, -40.5], [-7.0, -38.5], [-6.5, -38.5], [-6.5, -37.5], [-6.0, -37.5], [-6.0, -39.0], [-5.5, -39.0], [-5.5, -40.5], [-5.0, -40.5], [-5.0, -42.0], [-4.5, -42.0], [-4.5, -42.5], [-3.0, -42.5], [-3.0, -41.5], [-2.5, -41.5], [-2.5, -40.0], [-2.0, -40.0], [-2.0, -38.0], [-1.5, -38.0], [-1.5, -36.5], [-1.0, -36.5], [-1.0, -34.5], [-0.5, -34.5], [-0.5, -34.0], [-2.0, -34.0], [-2.0, -34.5], [-2.5, -34.5], [-2.5, -36.5], [-3.0, -36.5], [-3.0, -38.0], [-3.5, -38.0], [-3.5, -39.0], [-4.0, -39.0], [-4.0, -37.5], [-4.5, -37.5], [-4.5, -35.5], [-5.0, -35.5], [-5.0, -34.0], [-7.0, -34.0], [-7.0, -35.0], [-7.5, -35.0], [-7.5, -36.5], [-8.0, -36.5], [-8.0, -38.5], [-9.0, -38.5], [-9.0, -37.0], [-9.5, -37.0], [-9.5, -35.5], [-10.0, -35.5], [-10.0, -34.0], [-12.0, -34.0]]]}, "type": "Feature", "properties": {"raster_val": 0}}, {"geometry": {"type": "Polygon", "coordinates": [[[9.5, -34.0], [9.5, -34.5], [9.0, -34.5], [8.5, -34.5], [8.5, -35.0], [8.0, -35.0], [8.0, -42.5], [9.5, -42.5], [9.5, -36.0], [10.0, -36.0], [10.0, -35.5], [12.0, -35.5], [12.5, -35.5], [12.5, -34.5], [13.0, -34.5], [13.0, -34.0], [9.5, -34.0]]]}, "type": "Feature", "properties": {"raster_val": 0}}, {"geometry": {"type": "Polygon", "coordinates": [[[15.0, -34.0], [15.0, -34.5], [14.5, -34.5], [14.5, -35.0], [14.0, -35.0], [14.0, -36.0], [13.5, -36.0], [13.5, -40.5], [14.0, -40.5], [14.0, -41.5], [14.5, -41.5], [14.5, -42.0], [15.0, -42.0], [15.0, -42.5], [19.0, -42.5], [19.0, -42.0], [19.5, -42.0], [20.0, -42.0], [20.0, -40.5], [19.0, -40.5], [19.0, -41.0], [18.5, -41.0], [17.5, -41.0], [17.5, -41.5], [17.0, -41.5], [17.0, -41.0], [16.0, -41.0], [16.0, -40.5], [15.5, -40.5], [15.5, -38.5], [19.5, -38.5], [20.0, -38.5], [20.0, -36.0], [19.5, -36.0], [19.5, -35.0], [19.0, -35.0], [19.0, -34.5], [18.5, -34.5], [18.5, -34.0], [15.0, -34.0]], [[16.0, -35.5], [16.0, -36.0], [15.5, -36.0], [15.5, -37.0], [17.5, -37.0], [18.0, -37.0], [18.0, -36.0], [17.5, -36.0], [17.5, -35.5], [16.0, -35.5]]]}, "type": "Feature", "properties": {"raster_val": 0}}, {"geometry": {"type": "Polygon", "coordinates": [[[1.5, -34.0], [1.5, -34.5], [1.0, -34.5], [1.0, -35.0], [0.5, -35.0], [0.5, -35.5], [0.0, -35.5], [0.0, -37.0], [-0.5, -37.0], [-0.5, -39.5], [0.0, -39.5], [0.0, -41.0], [0.5, -41.0], [0.5, -41.5], [1.0, -41.5], [1.0, -42.0], [1.5, -42.0], [1.5, -42.5], [5.5, -42.5], [5.5, -42.0], [6.0, -42.0], [6.5, -42.0], [6.5, -41.0], [6.0, -41.0], [6.0, -40.5], [5.5, -40.5], [5.5, -41.0], [2.5, -41.0], [2.5, -40.5], [2.0, -40.5], [2.0, -39.5], [1.5, -39.5], [1.5, -38.5], [6.0, -38.5], [6.5, -38.5], [6.5, -37.0], [6.0, -37.0], [6.0, -35.5], [5.5, -35.5], [5.5, -34.5], [4.5, -34.5], [4.5, -34.0], [1.5, -34.0]], [[1.5, -36.5], [1.5, -37.0], [4.0, -37.0], [4.5, -37.0], [4.5, -36.0], [4.0, -36.0], [4.0, -35.5], [2.0, -35.5], [2.0, -36.5], [1.5, -36.5]]]}, "type": "Feature", "properties": {"raster_val": 0}}, {"geometry": {"type": "Polygon", "coordinates": [[[-18.0, -34.0], [-18.0, -34.5], [-18.5, -34.5], [-18.5, -35.0], [-19.0, -35.0], [-19.0, -35.5], [-19.5, -35.5], [-19.5, -36.0], [-20.0, -36.0], [-20.0, -40.0], [-19.5, -40.0], [-19.5, -41.0], [-19.0, -41.0], [-19.0, -41.5], [-18.5, -41.5], [-18.5, -42.0], [-18.0, -42.0], [-18.0, -42.5], [-14.5, -42.5], [-14.5, -42.0], [-14.0, -42.0], [-13.5, -42.0], [-13.5, -41.5], [-13.0, -41.5], [-13.0, -40.5], [-12.5, -40.5], [-12.5, -36.0], [-13.0, -36.0], [-13.0, -35.0], [-13.5, -35.0], [-13.5, -34.5], [-14.5, -34.5], [-14.5, -34.0], [-18.0, -34.0]], [[-16.0, -41.0], [-15.5, -41.0], [-15.0, -41.0], [-15.0, -40.5], [-14.5, -40.5], [-14.5, -36.0], [-15.0, -36.0], [-15.0, -35.5], [-17.5, -35.5], [-17.5, -36.5], [-18.0, -36.5], [-18.0, -40.0], [-17.5, -40.0], [-17.5, -41.0], [-16.5, -41.0], [-16.5, -41.5], [-16.0, -41.5], [-16.0, -41.0]]]}, "type": "Feature", "properties": {"raster_val": 0}}, {"geometry": {"type": "Polygon", "coordinates": [[[27.0, -29.5], [27.0, -30.0], [26.5, -30.0], [26.5, -34.0], [26.0, -34.0], [23.0, -34.0], [23.0, -34.5], [22.5, -34.5], [22.5, -35.0], [22.0, -35.0], [22.0, -35.5], [21.5, -35.5], [21.5, -36.5], [21.0, -36.5], [21.0, -40.0], [21.5, -40.0], [21.5, -41.0], [22.0, -41.0], [22.0, -41.5], [22.5, -41.5], [22.5, -42.0], [23.0, -42.0], [23.0, -42.5], [26.5, -42.5], [26.5, -42.0], [27.0, -42.0], [27.5, -42.0], [27.5, -41.5], [28.0, -41.5], [28.5, -41.5], [28.5, -29.5], [27.0, -29.5]], [[25.5, -41.0], [25.5, -41.5], [24.5, -41.5], [24.5, -41.0], [24.0, -41.0], [24.0, -40.5], [23.5, -40.5], [23.5, -39.5], [23.0, -39.5], [23.0, -37.0], [23.5, -37.0], [23.5, -36.0], [24.0, -36.0], [24.0, -35.5], [26.5, -35.5], [26.5, -41.0], [26.0, -41.0], [25.5, -41.0]]]}, "type": "Feature", "properties": {"raster_val": 0}}, {"geometry": {"type": "Polygon", "coordinates": [[[-26.5, -34.0], [-26.5, -34.5], [-27.0, -34.5], [-27.5, -34.5], [-27.5, -35.0], [-28.0, -35.0], [-28.0, -46.5], [-26.5, -46.5], [-26.5, -42.0], [-26.0, -42.0], [-26.0, -42.5], [-22.5, -42.5], [-22.5, -42.0], [-22.0, -42.0], [-22.0, -41.5], [-21.5, -41.5], [-21.5, -41.0], [-21.0, -41.0], [-21.0, -38.5], [-20.5, -38.5], [-20.5, -38.0], [-21.0, -38.0], [-21.0, -36.0], [-21.5, -36.0], [-21.5, -35.0], [-22.0, -35.0], [-22.0, -34.5], [-22.5, -34.5], [-22.5, -34.0], [-26.5, -34.0]], [[-26.0, -35.5], [-26.0, -36.0], [-26.5, -36.0], [-26.5, -40.5], [-26.0, -40.5], [-26.0, -41.0], [-23.5, -41.0], [-23.5, -40.5], [-23.0, -40.5], [-23.0, -36.0], [-23.5, -36.0], [-23.5, -35.5], [-26.0, -35.5]]]}, "type": "Feature", "properties": {"raster_val": 0}}]}
# python_powered.py
#
# Turns the 15 features in the Python logo
#
# http://www.python.org/community/logos/python-powered-h-140x182.png
#
# into a GeoJSON feature collection.
import json
import subprocess
import rasterio
from rasterio import features
with rasterio.drivers():
with rasterio.open('python-powered-h-140x182.png') as src:
blue = src.read_band(3)
# Set every non-background pixel to 0 and then mask out the
# white background.
blue[blue < 255] = 0
mask = blue != 255
# transform to world coordinates so that we can map it
transform = [-35.0, 0.5, 0.0, 40.5, 0.0, -0.5]
# Get shapes from the positive part of the image.
shapes = list(features.shapes(blue, mask=mask, transform=transform))
# Burn those shapes into a new image.
image = features.rasterize(
((g, 255) for g, v in shapes),
out_shape=blue.shape, fill=0, transform=transform )
# Write the new image out.
kwargs = src.meta
kwargs['count'] = 1
kwargs['driver'] = 'GTiff'
with rasterio.open('result.tif', 'w', **kwargs) as dst:
dst.write_band(1, image)
subprocess.call(['open', 'result.tif'])
# Write the shapes out to GeoJSON.
results = ({
'type': 'Feature',
'properties': {'raster_val': v},
'geometry': s }
for i, (s, v)
in enumerate(shapes) )
collection = {
'type': 'FeatureCollection',
'features': list(results) }
with open('python-powered.json', 'w') as dst:
json.dump(collection, dst)
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@jseppi
Copy link

jseppi commented Jan 31, 2014

Did you use a world file or some other projection source for your source image? For me, GDAL reports ERROR 6: No translation an empty SRS to PROJ.4 format is known. and I get a pretty whacky geojson representation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment