Created
August 23, 2018 13:32
-
-
Save camallen/17d4b244d8439f129b5f2e9083448a46 to your computer and use it in GitHub Desktop.
Convert csv RGB layer data to geojson points
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
import csv, json, pdb; | |
from geojson import Feature, FeatureCollection, Point | |
def convertBox2MidPoint(lower_lat, lower_lon, upper_lat, upper_lon): | |
delta_lon = abs(lower_lon - upper_lon) / 2 | |
delta_lat = abs(lower_lat - upper_lat) / 2 | |
mid_lon = lower_lon + delta_lon | |
mid_lat = lower_lat + delta_lat | |
# geojson is lon, lat ordering | |
return (mid_lon, mid_lat) | |
features = [] | |
with open('csv/overlay_0.csv') as csvfile: | |
# csv.QUOTE_NONNUMERIC -> convert non quoted inputs to floats | |
reader = csv.reader(csvfile, quoting=csv.QUOTE_NONNUMERIC) | |
for row in reader: | |
# only iterested in the red channel...as it's the only data | |
# lower_* is left, upper_* is right in the source data | |
# skip null data | |
intensity = row[4] | |
if intensity == 0: | |
continue | |
# csv format is lower_lat, lower_long, upper_lat, upper_long | |
result_tuple = convertBox2MidPoint(*row[0:4]) | |
features.append( | |
Feature( | |
geometry = Point(result_tuple), | |
properties = { | |
'intensity': intensity | |
} | |
) | |
) | |
collection = FeatureCollection(features) | |
with open("geo_json/overlay_0.json", "w") as file: | |
file.write('%s' % collection) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment