Select to view content in your preferred language

Location Update through CSV file using Python

936
2
02-02-2024 01:35 AM
MariaJutt
New Contributor

I want to update a location using a csv file, the sender will send his location in a csv file and at the receiver end i will be running a python script to check any incoming received file in my folder as soon as a new file comes in the received folder the location will be automatically updated. the code written for this is as under.

import arcpy
import pandas as pd
import os
from glob import glob

# Base path for CSV files
base_csv_path = r"C:\Users\saim\Desktop\EthCom_V_3.21\ReceivedFiles"

# Feature layer path
feature_layer_path = r"C:\Users\saim\Documents\ArcGIS\Projects\MyProject3\MyProject3.gdb\Yesss"

# Function to update feature layer with points from a CSV file
def update_feature_layer(csv_file_path):
timestamp_column = "Timestamp"
lat_column = "Latitude"
lon_column = "Longitude"
altitude_column = "Altitude"

# Read CSV data
data = pd.read_csv(csv_file_path)

# Create a spatial reference object for WGS84 (latitude and longitude coordinates)
spatial_reference = arcpy.SpatialReference(4326)

# Create PointGeometry objects
point_array = [arcpy.PointGeometry(arcpy.Point(float(lon), float(lat)), spatial_reference) for lat, lon in zip(data[lat_column], data[lon_column])]

# Insert points into the feature layer
with arcpy.da.InsertCursor(feature_layer_path, ["SHAPE@"]) as cursor:
for point in point_array:
cursor.insertRow([point])

print(f"Updated feature layer with data from {csv_file_path}")

# List of receivers
receivers = ["truck_1", "truck_2", "truck_3"]

# Loop through receivers and update the feature layer with the latest CSV file
for receiver in receivers:
search_pattern = os.path.join(base_csv_path, f"{receiver}*.csv")
latest_csv_file = max(glob(search_pattern), key=os.path.getctime, default=None)

if latest_csv_file:
update_feature_layer(latest_csv_file)
print(f"Updated feature layer with data from {latest_csv_file}")
else:
print(f"No CSV file found for {receiver}")

but the issue is that the code successfully runs but there is no point being shown on the map. nothing is visible on the layer created for it

0 Kudos
2 Replies
DanPatterson
MVP Esteemed Contributor

Code formatting ... the Community Version - Esri Community

to facilitate reading and to provide line numbers


... sort of retired...
0 Kudos
Raul
by
Regular Contributor

Your script (with proper indentation) works for me! Check the attribute table to confirm features are being created. Also check if you switched longitude and latitude values (I know I did and my point initially ended up in Antarctica 😅)

0 Kudos