Instrução
No ArcGIS Pro, as ferramentas de geoprocessamento podem ser utilizadas para criar polígonos referenciando os dados de coordenadas de ponto x e y de um arquivo CSV. Esse processo também pode ser automatizado usando o módulo ArcPy. Siga o fluxo de trabalho descrito neste artigo para criar polígonos a partir de um arquivo CSV usando um arquivo Python independente (.py). O script Python também é executado no Jupyter Notebook e na janela do Python.
Note: All field values in the CSV file must be numeric data type. If there are text field values, the Python script fails.
import csv import os import arcpy
in_dir = r"<CSVFileFolderPath>" csvFile = "<fileName>.csv" fc = "<newShapefileName>" in_csv = os.path.join(in_dir, csvFile) sr = arcpy.SpatialReference(<projectedCoordinateSystemWKID>)
arcpy.management.CreateFeatureclass( in_dir, fc, geometry_type="POLYGON", spatial_reference=sr ) out_feature_class = os.path.join(in_dir, fc)
arcpy.management.AddField(out_feature_class, "<fieldName>", "LONG")
with arcpy.da.InsertCursor(out_feature_class, ["SHAPE@", "<fieldName>"]) as cursor: point_list = [] parcel_value = None is_first_pass = True with open(in_csv, mode="r") as csv_file: csv_reader = csv.reader(csv_file, delimiter=",") header = next(csv_reader) for row in csv_reader: if is_first_pass: parcel_value = row[0] is_first_pass = False elif row[0] != parcel_value: polygon = arcpy.Polygon(arcpy.Array(point_list), sr) cursor.insertRow([polygon, parcel_value]) parcel_value = row[0] point_list = [] point_list.append(arcpy.Point(row[1], row[2])) polygon = arcpy.Polygon(arcpy.Array(point_list), sr) cursor.insertRow([polygon, parcel_value])
print("COMPLETED")
O bloco de código abaixo demonstra o funcionamento completo do script.
import csv import os import arcpy in_dir = r"C:\Users\ISC-DT25\Documents\Artwork\Art 29483" csvFile = "par_point-TEST.csv" fc = "polygonFromCSV" in_csv = os.path.join(in_dir, csvFile) sr = arcpy.SpatialReference(32049) arcpy.management.CreateFeatureclass( in_dir, fc, geometry_type="POLYGON", spatial_reference=sr ) out_feature_class = os.path.join(in_dir, fc) arcpy.management.AddField(out_feature_class, "POINTID", "LONG") with arcpy.da.InsertCursor(out_feature_class, ["SHAPE@", "POINTID"]) as cursor: point_list = [] parcel_value = None is_first_pass = True with open(in_csv, mode="r") as csv_file: csv_reader = csv.reader(csv_file, delimiter=",") header = next(csv_reader) for row in csv_reader: if is_first_pass: parcel_value = row[0] is_first_pass = False elif row[0] != parcel_value: polygon = arcpy.Polygon(arcpy.Array(point_list), sr) cursor.insertRow([polygon, parcel_value]) parcel_value = row[0] point_list = [] point_list.append(arcpy.Point(row[1], row[2])) polygon = arcpy.Polygon(arcpy.Array(point_list), sr) cursor.insertRow([polygon, parcel_value]) print("COMPLETED")
Obtenha ajuda de especialistas do ArcGIS
Baixe o Aplicativo de Suporte da Esri