Hi,
I am trying to generate a shape file out of pointFile.
The pointFile has 5 records in it, each records is a point.
it has 5 columns for each records, id,ShapeID,x,y,z. id and shapeID doesn't mean anything.
All these points form a polygon as you can see, the first record is same as the last
record.
Program: I wrote a python program that will read the pointFile and outputs a shapeFile.
There are two methods in it, 1) _create_polyline_shapefile 2) _create_polygon_shapefile
_create_polyline_shapefile will read the points and outputs a Line File.
_create_polygon_shapefile will read the points and outputs a Polygon File.
Problem: what we are facing is the output of _create_polyline_shapefile does not show graphics in ArcMap or ArcScene.
while the one created by first methods displays. The shape file can be viewed in ArcScene for 3dView or ArcMap for 2D view.
looking for a solution for this.
Here are 5 records of the in the PointFile
======================
id, shapeId, x, y, z
1,1,2197490.3821680555,1.5003079009938888E7,1573.2872741540364
2,2,2197478.7284111492,1.5003076194199622E7,1570.29
3,3,2197459.2878189506,1.500307149703293E7,1570.29
4,4,2197446.2932236306,1.5003068357325058E7,1573.6321294959107
5,5,2197490.3821680555,1.5003079009938888E7,1573.2872741540364
Here is the python program
===================
import arcpy
import os
class TextToShapefileConverter:
def __init__(self, input_directory, output_directory, spatial_reference_id=26914😞
self.input_directory = input_directory
self.output_directory = output_directory
self.spatial_reference_id = spatial_reference_id
self.spatial_reference = arcpy.SpatialReference(self.spatial_reference_id)
def convert(self, point_file_name):
input_text_file = os.path.join(self.input_directory, point_file_name)
output_shapefile_name = os.path.splitext(point_file_name)[0] + '.shp'
output_shapefile = os.path.join(self.output_directory, output_shapefile_name)
print "generating", output_shapefile_name
if "PolygonPointFile" in point_file_name:
self._create_polyline_shapefile(output_shapefile, input_text_file)
# self._create_polygon_shapefile(output_shapefile, input_text_file)
def _create_polyline_shapefile(self, output_shapefile, input_text_file):
arcpy.env.overwriteOutput = True
arcpy.CreateFeatureclass_management(
os.path.dirname(output_shapefile),
os.path.basename(output_shapefile),
"POLYLINE", has_z="ENABLED", has_m="DISABLED",
spatial_reference=self.spatial_reference)
arcpy.AddField_management(output_shapefile, "PairID", "LONG")
cursor = arcpy.da.InsertCursor(output_shapefile, ['SHAPE@', 'PairID'])
points = []
pair_id = 0
with open(input_text_file, 'r') as file:
next(file) # Skip header
for line in file:
if line.strip():
_, _, x, y, z = line.split(',')
point = arcpy.Point(float(x), float(y), float(z))
points.append(point)
# Check if a pair of points has been added
if len(points) == 5:
print points
polyline = arcpy.Polyline(arcpy.Array(points), self.spatial_reference, True)
cursor.insertRow([polyline, pair_id])
points = [] # Reset for next pair
pair_id += 1
del cursor
def _create_polygon_shapefile(self, output_shapefile, input_text_file):
arcpy.env.overwriteOutput = True
arcpy.CreateFeatureclass_management(
os.path.dirname(output_shapefile),
os.path.basename(output_shapefile),
"POLYGON", has_z="ENABLED", has_m="DISABLED",
spatial_reference=self.spatial_reference)
arcpy.AddField_management(output_shapefile, "GroupID", "LONG")
cursor = arcpy.da.InsertCursor(output_shapefile, ['SHAPE@', 'GroupID'])
points = []
group_id = 0
with open(input_text_file, 'r') as file:
next(file) # Skip header
for line in file:
if line.strip():
id, _, x, y, z = line.split(',')
point = arcpy.Point(float(x), float(y), float(z))
points.append(point)
if len(points) == 5:
if points[0] != points[-1]:
points.append(points[0]) # Ensure the polygon is closed
polygon = arcpy.Polygon(arcpy.Array(points), self.spatial_reference, True)
cursor.insertRow([polygon, group_id])
points = [] # Reset for next group
group_id += 1
del cursor
if __name__ == "__main__":
input_directory = r'C:\Tutorial\GIS\ArcPyTutorial\Data\MyTestFolder'
output_directory = r'C:\Tutorial\GIS\ArcPyTutorial\Data\MyTestFolder\outPut'
converter = TextToShapefileConverter(input_directory, output_directory)
# Process each file
for filename in os.listdir(input_directory):
if filename.endswith('.txt'😞
converter.convert(filename)