In [1]:
#author HÃ¥kon, Tiril, Helene og Tom
import os
import sys
import time, datetime
import arcpy
from zipfile import ZipFile
from login_utils import password, user
from arcgis.gis import GIS
from arcgis.features import FeatureLayerCollection
from time import strftime
from shutil import copyfile

Workspace

In [2]:
arcpy.env.workspace = "C:/ProgramData/Anaconda3/envs/KDE/default.gdb"
arcpy.env.overwriteOutput = True

GPX TO shape and LINE

In [3]:
#GPX filer
gpx_filepath = r"C:\Users\hauke\bacherlor2020\linjer\GPXfiler"



#variabler
lineField = ""
sortField = ""

# GPX til punkter og linjer
gpx_list = os.listdir(gpx_filepath)
for gpx_file in gpx_list:
    if gpx_file.endswith(".gpx"):
        full_gpx_path = os.path.join(gpx_filepath, gpx_file)
        output_shapefile = os.path.splitext(full_gpx_path)[0] + ".shp"
        try:
            arcpy.GPXtoFeatures_conversion(full_gpx_path,
                                           output_shapefile)
        except Exception as e:
            print(e)
        try:
            arcpy.PointsToLine_management(output_shapefile,
                                          gpx_filepath + gpx_file,
                                          lineField,
                                          sortField)
        except Exception as e:
            print(e)



#print("finished all converting all gpx to shapefile")
Failed to execute. Parameters are not valid.
ERROR 000732: Input Features: Dataset C:\Users\hauke\bacherlor2020\linjer\GPXfiler\Lunch_Ride (1).shp does not exist or is not supported
Failed to execute (PointsToLine).

Merged Lines

In [4]:
#variabler
arcpy.env.workspace = r"C:\Users\hauke\bacherlor2020\linjer"
fc = arcpy.ListFeatureClasses(feature_type = 'line')
output = r"C:\Users\hauke\bacherlor2020\merged_lines\merged.shp"
addSourceInfo = "test"

#merge lines
arcpy.Merge_management(fc, output, addSourceInfo)
Out[4]:

Output

C:\Users\hauke\bacherlor2020\merged_lines\merged.shp

Messages

Start Time: onsdag 19. mai 2021 14:42:03
Succeeded at onsdag 19. mai 2021 14:42:05 (Elapsed Time: 1,35 seconds)

Buffer merged lines

In [5]:
arcpy.env.workspace = r"C:/ProgramData/Anaconda3/envs/KDE/default.gdb"
arcpy.env.overwriteOutput = True
#output = r"C:\Users\hauke\bacherlor2020\merged_lines\merged.shp"


in_features = output
out_feature_class =  r"C:/ProgramData/Anaconda3/envs/KDE/default.gdb/buffer10m"



arcpy.Buffer_analysis(in_features,
                      out_feature_class,
                      buffer_distance_or_field="10 Meters",
                      line_side="FULL",
                      line_end_type="ROUND",
                      dissolve_option="NONE",
                      dissolve_field=[],
                      method="PLANAR")
Out[5]:

Output

C:\ProgramData\Anaconda3\envs\KDE\Default.gdb\buffer10m

Messages

Start Time: onsdag 19. mai 2021 14:42:05
Succeeded at onsdag 19. mai 2021 14:42:08 (Elapsed Time: 3,45 seconds)

Aggregate Polygons

In [6]:
arcpy.env.workspace = r"C:/ProgramData/Anaconda3/envs/KDE/default.gdb"
arcpy.env.overwriteOutput = True

#variabler
buffer = out_feature_class
buffer_AggregatePolygons = r"C:/ProgramData/Anaconda3/envs/KDE/default.gdb/buffer10m_aggregated"
buffer_AggregatePolygons_Tbl = "yikes"




arcpy.cartography.AggregatePolygons(in_features=buffer,
                                    out_feature_class=buffer_AggregatePolygons,
                                    aggregation_distance="10 Meters",
                                    minimum_area="0 Unknown",
                                    minimum_hole_size="0 Unknown",
                                    orthogonality_option="NON_ORTHOGONAL",
                                    barrier_features=[],
                                    out_table=buffer_AggregatePolygons_Tbl,
                                    aggregate_field="")
Out[6]:

Output

idvalue
0C:\ProgramData\Anaconda3\envs\KDE\Default.gdb\buffer10m_aggregated
1C:/ProgramData/Anaconda3/envs/KDE/Default.gdb\yikes

Messages

Start Time: onsdag 19. mai 2021 14:42:09
Finding polygon clusters...
Constructing polygons (non_orthogonal)...
Succeeded at onsdag 19. mai 2021 14:44:08 (Elapsed Time: 1 minutes 58 seconds)

Polygon to Centerline

In [7]:
#variabler
inputLyr = r"C:/ProgramData/Anaconda3/envs/KDE/default.gdb/buffer10m_aggregated"
out_feature_class1 = r"C:/ProgramData/Anaconda3/envs/KDE/default.gdb/centerline_aggregated1"


arcpy.topographic.PolygonToCenterline(inputLyr, out_feature_class1)
arcpy.env.overwriteOutput = True

Trim Lines

In [8]:
arcpy.TrimLine_edit(out_feature_class1, "150 Meters", "DELETE_SHORT")
Out[8]:

Output

C:/ProgramData/Anaconda3/envs/KDE/default.gdb/centerline_aggregated1

Messages

Start Time: onsdag 19. mai 2021 14:44:13
Reading Features...
Cracking Features...
Assembling Features...
Succeeded at onsdag 19. mai 2021 14:44:15 (Elapsed Time: 2,37 seconds)

Feature to shape

In [9]:
centerline_aggregated1 = out_feature_class1
senterlinje_2_ = r"C:\Users\hauke\bacherlor2020\senterlinje"
senterlinje = arcpy.conversion.FeatureClassToShapefile(Input_Features=[centerline_aggregated1],
                                                       Output_Folder=senterlinje_2_)

Featurelayer - GDB(ONLINE)

In [10]:
from arcgis.gis import GIS
gis = GIS('Home')

#link til mappe
data_dir = r"C:\Users\hauke\bacherlor2020\senterlinje"

#Get list of all files
file_list = os.listdir(data_dir)
sd_file_list = [x for x in file_list if x.endswith(".shp")]
print("Number of .shp files found: " + str(len(sd_file_list)))



#arcgisoneline login
gis = GIS('https://ntnu-gis.maps.arcgis.com/', user, password)

#variables
itemid = '697baf4054c147339c544f0b782e4134'
upload_zip = r"C:\Users\hauke\bacherlor2020\senterlinje\sipsip.zip"
dld_path = r'C:\Users\hauke\test\bachelor\f'


# create a ZipFile object
zipObj = ZipFile(r'C:\Users\hauke\bacherlor2020\senterlinje\sipsip.zip', 'w')
zipObj.write(r"C:\Users\hauke\bacherlor2020\senterlinje\centerline_aggregated1.cpg")
zipObj.write(r"C:\Users\hauke\bacherlor2020\senterlinje\centerline_aggregated1.dbf")
zipObj.write(r"C:\Users\hauke\bacherlor2020\senterlinje\centerline_aggregated1.prj")
zipObj.write(r"C:\Users\hauke\bacherlor2020\senterlinje\centerline_aggregated1.sbn")
zipObj.write(r"C:\Users\hauke\bacherlor2020\senterlinje\centerline_aggregated1.sbx")
zipObj.write(r"C:\Users\hauke\bacherlor2020\senterlinje\centerline_aggregated1.shp")
zipObj.write(r"C:\Users\hauke\bacherlor2020\senterlinje\centerline_aggregated1.shp.xml")
zipObj.write(r"C:\Users\hauke\bacherlor2020\senterlinje\centerline_aggregated1.shx")
zipObj.close()
#print login
un = gis.properties.user.username
print('Logged in as : {}'.format(un))                       

#overwrite feature host layer
dataitem = gis.content.get(itemid)
flc = FeatureLayerCollection.fromitem(dataitem)
flc.manager.overwrite(upload_zip)
Number of .shp files found: 3
Logged in as : haakhegr_NTNU_2020
Out[10]:
{'success': True}

remove shapefiles

In [12]:
os.remove(r"C:\Users\hauke\bacherlor2020\senterlinje\centerline_aggregated1.cpg")
os.remove(r"C:\Users\hauke\bacherlor2020\senterlinje\centerline_aggregated1.dbf")
os.remove(r"C:\Users\hauke\bacherlor2020\senterlinje\centerline_aggregated1.prj")
os.remove(r"C:\Users\hauke\bacherlor2020\senterlinje\centerline_aggregated1.sbn")
os.remove(r"C:\Users\hauke\bacherlor2020\senterlinje\centerline_aggregated1.sbx")
os.remove(r"C:\Users\hauke\bacherlor2020\senterlinje\centerline_aggregated1.shp")
os.remove(r"C:\Users\hauke\bacherlor2020\senterlinje\centerline_aggregated1.shp.xml")
os.remove(r"C:\Users\hauke\bacherlor2020\senterlinje\centerline_aggregated1.shx")
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-12-c889ca4c8633> in <module>
----> 1 os.remove(r"C:\Users\hauke\bacherlor2020\senterlinje\centerline_aggregated1.cpg")
      2 os.remove(r"C:\Users\hauke\bacherlor2020\senterlinje\centerline_aggregated1.dbf")
      3 os.remove(r"C:\Users\hauke\bacherlor2020\senterlinje\centerline_aggregated1.prj")
      4 os.remove(r"C:\Users\hauke\bacherlor2020\senterlinje\centerline_aggregated1.sbn")
      5 os.remove(r"C:\Users\hauke\bacherlor2020\senterlinje\centerline_aggregated1.sbx")

FileNotFoundError: [WinError 2] Systemet finner ikke angitt fil: 'C:\\Users\\hauke\\bacherlor2020\\senterlinje\\centerline_aggregated1.cpg'
In [ ]: