|
| 1 | +import click |
| 2 | +import csv |
| 3 | +import json |
| 4 | +import re |
| 5 | +from datetime import datetime |
| 6 | +from shapely.wkb import loads |
| 7 | +from shapely.geometry import Point |
| 8 | + |
| 9 | + |
| 10 | +import re |
| 11 | +from datetime import datetime |
| 12 | + |
| 13 | +def convert_to_unix_time(datetime_str): |
| 14 | + """Convert a datetime string to a Unix timestamp, extracting only YYYY-MM-DD HH:MM:SS.""" |
| 15 | + try: |
| 16 | + # Step 1: Use regex to extract the part of the datetime string in the format YYYY-MM-DD HH:MM:SS |
| 17 | + match = re.match(r'(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})', datetime_str) |
| 18 | + if match: |
| 19 | + datetime_str = match.group(0) # Extract the matched part |
| 20 | + |
| 21 | + # Step 2: Convert the string to a datetime object |
| 22 | + dt = datetime.strptime(datetime_str, "%Y-%m-%d %H:%M:%S") |
| 23 | + |
| 24 | + # Step 3: Return Unix timestamp |
| 25 | + return int(dt.timestamp()) |
| 26 | + |
| 27 | + except ValueError as e: |
| 28 | + print(f"Error parsing datetime: {datetime_str}") |
| 29 | + raise e |
| 30 | + |
| 31 | +@click.command() |
| 32 | +@click.argument('csv_file', type=click.Path(exists=True)) |
| 33 | +@click.argument('geojson_file', type=click.Path()) |
| 34 | +def csv_to_geojson(csv_file, geojson_file): |
| 35 | + """Convert CSV to GeoJSON with specific formatting and handling of NULL values.""" |
| 36 | + |
| 37 | + features = [] |
| 38 | + |
| 39 | + with open(csv_file, newline='', encoding='utf-8') as csvfile: |
| 40 | + reader = csv.DictReader(csvfile) |
| 41 | + |
| 42 | + for row in reader: |
| 43 | + # Skip row if coordinates are NULL |
| 44 | + if row['coordinates'] == 'NULL': |
| 45 | + continue |
| 46 | + |
| 47 | + # Parse WKT coordinates into GeoJSON Point |
| 48 | + point = loads(row['coordinates']) |
| 49 | + |
| 50 | + # Create properties dictionary |
| 51 | + properties = {} |
| 52 | + for key, value in row.items(): |
| 53 | + if value != 'NULL': |
| 54 | + if key == 'start_datetime': |
| 55 | + # Add original start_datetime |
| 56 | + properties['start_datetime'] = value |
| 57 | + print(value) |
| 58 | + # Convert to unix_time and add it to properties |
| 59 | + properties['unix_time'] = convert_to_unix_time(value) |
| 60 | + elif key in ['depth', 'magnitude']: |
| 61 | + # Set to 0 if value is NULL |
| 62 | + properties[key] = float(value) if value != 'NULL' else 0 |
| 63 | + else: |
| 64 | + properties[key] = value |
| 65 | + |
| 66 | + # Create feature |
| 67 | + feature = { |
| 68 | + "type": "Feature", |
| 69 | + "geometry": { |
| 70 | + "type": "Point", |
| 71 | + "coordinates": [point.x, point.y] |
| 72 | + }, |
| 73 | + "properties": properties |
| 74 | + } |
| 75 | + |
| 76 | + features.append(feature) |
| 77 | + |
| 78 | + # Create the GeoJSON structure |
| 79 | + geojson = { |
| 80 | + "type": "FeatureCollection", |
| 81 | + "features": features |
| 82 | + } |
| 83 | + |
| 84 | + # Write to output GeoJSON file |
| 85 | + with open(geojson_file, 'w', encoding='utf-8') as outfile: |
| 86 | + json.dump(geojson, outfile, indent=4) |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + csv_to_geojson() |
0 commit comments