Example #1
0
def get_final_tiles(fishnet_out):
    '''(full path to fishnet shapefile)-> dict
    Input: Fishnet feature class.
    Iterates over each feature in the the feature class, creating temporary
    polygon objects with extents equal to each feature extent. These will be used as final
    processing extents for clipping the buffered tiles.
    Output: Dict with tile names as keys and polygon objects as values.
    '''
    with arcpy.da.SearchCursor(fishnet_out, ['Name', 'SHAPE@']) as rows:
        tile_dict = {}
        array = arcpy.Array()
        for row in rows:
            name = row[0]
            extent = row[1].extent
            array.add(extent.lowerLeft)
            array.add(extent.lowerRight)
            array.add(extent.upperRight)
            array.add(extent.upperLeft)
            tile_dict[name] = arcpy.Polygon(array)
            array.removeAll()
            del row
        del array
    return tile_dict
def image2features(img_bat, features, lat, lon):
    """
    Create a list of features
    :param img_bat: (grayscale image) Image of buildings
    :param features: (list) List of Polygon objects
    :param lat: (float) Latitude of the screenshot URL
    :param lon: (float) Longitude of the screenshot URL
    """
    #  create contours
    im2, contours, hierarchy = cv.findContours(img_bat, cv.RETR_TREE,
                                               cv.CHAIN_APPROX_SIMPLE)

    # create array of points
    polygones = [[] for _ in contours]
    points = []
    list_coord_poly = []
    for i in range(len(contours)):
        for j in range(len(contours[i])):
            var = np.array(contours[i][j]).tolist()
            points.append(var[0])
        polygones[i].append(points[:])
        points = []
        list_coord_poly.append(polygones[i][0])

    # project points from geographic coordinates(Google Maps) to WGS_1984_Web_Mercator_Auxiliary_Sphere EPSG:3857
    list_coord_poly = inv_y(list_coord_poly, img_bat)
    list_coord_poly = scale(list_coord_poly)
    list_coord_poly = translation(list_coord_poly, lat, lon)

    # list of Polygon objects
    # features = []
    for polygone in list_coord_poly:
        # Create a Polygon object based on the array of points
        # Append to the list of Polygon objects
        features.append(
            arcpy.Polygon(
                arcpy.Array([arcpy.Point(*coords) for coords in polygone])))
Example #3
0
def create_image_extent_FC(inputRaster, input_sr, tmpGDB):
    arcpy.AddMessage('Start creating image"s extent polygon...')
    starto = timeit.default_timer()
    if input_sr.linearUnitCode == 0:  ### if input_sr is GCS find PCS for it
        input_sr_pcs = GetUTMFromRaster(inputRaster)
    else:
        input_sr_pcs = input_sr
    image_extent_FC_org = arcpy.CreateFeatureclass_management(
        tmpGDB, "image_extent_FC_org", "POLYGON", "", "DISABLED", "DISABLED",
        input_sr)
    cursor = arcpy.InsertCursor(image_extent_FC_org)
    point = arcpy.Point()
    coords = arcpy.Array()
    corners = ["lowerLeft", "lowerRight", "upperRight", "upperLeft"]
    feat = cursor.newRow()
    r = arcpy.Raster(inputRaster)
    for corner in corners:
        point.X = getattr(r.extent, "%s" % corner).X
        point.Y = getattr(r.extent, "%s" % corner).Y
        coords.add(point)
    coords.add(coords.getObject(0))
    polygon = arcpy.Polygon(coords)
    feat.shape = polygon
    cursor.insertRow(feat)
    del feat
    del cursor
    ### project the image extent polygon if the inpot sr is GCS
    if input_sr.linearUnitCode == 0:
        image_extent_FC = arcpy.Project_management(
            image_extent_FC_org, os.path.join(tmpGDB, 'image_extent_FC'),
            input_sr_pcs)
    else:
        image_extent_FC = image_extent_FC_org
    endo = timeit.default_timer()
    arcpy.AddMessage(('End creating image"s extent polygon. Duration:',
                      round(endo - starto, 4)))
    return image_extent_FC
def createInsetBox():
    """The bus mall inset covers a portion of the city center map so that
	needs to be reflected in the inset box, using the inflection point and the
	city center bound box create an fc that contains the inset box"""

    inflect_pt = {'x': 7649075, 'y': 686384}
    bkmk_dict = getBookmarkBbox(city_center_mxd, city_center_bkmk)

    geom_type = 'POLYGON'
    oregon_spn = arcpy.SpatialReference(2913)
    management.CreateFeatureclass(os.path.dirname(inset_box),
                                  os.path.basename(inset_box),
                                  geom_type,
                                  spatial_reference=oregon_spn)

    f_name, f_type = 'name', 'TEXT'
    management.AddField(inset_box, f_name, f_type)

    drop_field = 'Id'
    arcpy.management.DeleteField(inset_box, drop_field)

    i_fields = ['Shape@', f_name]
    i_cursor = da.InsertCursor(inset_box, i_fields)

    ap_array = arcpy.Array()
    ap_array.add(arcpy.Point(bkmk_dict['x-min'], bkmk_dict['y-min']))
    ap_array.add(arcpy.Point(bkmk_dict['x-min'], bkmk_dict['y-max']))
    ap_array.add(arcpy.Point(bkmk_dict['x-max'], bkmk_dict['y-max']))
    ap_array.add(arcpy.Point(bkmk_dict['x-max'], inflect_pt['y']))
    ap_array.add(arcpy.Point(inflect_pt['x'], inflect_pt['y']))
    ap_array.add(arcpy.Point(inflect_pt['x'], bkmk_dict['y-min']))
    # add first point again to close polygon
    ap_array.add(arcpy.Point(bkmk_dict['x-min'], bkmk_dict['y-min']))

    i_cursor.insertRow((arcpy.Polygon(ap_array), 'Portland City Center'))

    del i_cursor
def save_netmo_data_as_shapefile(esri_headers, netmo_data, shapefile, outpath):
    coordinate_system = "WGS 1984"
    # arcpy.CreateFeatureclass_management(outpath, shapefile, "POINT", spatial_reference=coordinate_system)  # display segments as points
    arcpy.CreateFeatureclass_management(outpath, shapefile, "POLYLINE", spatial_reference=coordinate_system)  # display segments as lines
    # Tell ESRI about each column (name and type)
    for esri_header in esri_headers:
        esri_header_type = get_esri_header_type(esri_header)
        arcpy.AddField_management(shapefile, esri_header, esri_header_type)
    # Create one ESRI row for each NetMo row
    cursor = arcpy.InsertCursor(shapefile, coordinate_system)
    id = 0
    for netmo_row in netmo_data:
        esri_row = cursor.newRow()
        x1 = y1 = x2 = y2 = None
        for header, value in netmo_row.iteritems():
            if value is not None:
                esri_row.setValue(header, value)
                if header == "StartLong":
                    x1 = value
                elif header == "StartLati":
                    y1 = value
                elif header == "EndLongit":
                    x2 = value
                elif header == "EndLatitu":
                    y2 = value
        if x1 is not None:
            poly_line = arcpy.Array()
            poly_line.add(arcpy.Point(x1, y1))
            poly_line.add(arcpy.Point(x2, y2))
            # esri_row.shape = arcpy.Point(x1, y1)  # example where the shape is a point instead of a line
            esri_row.shape = poly_line
            esri_row.id = id
            id += 1
            cursor.insertRow(esri_row)  # Only insert rows for which we have lat/long data
        del esri_row
    del cursor
    pass
Example #6
0
def connect(in_fc, out_fc, N=1, testing=False):
    """Run the analysis to form the closest point pairs.

    Calls n_near to produce the nearest features.
    """
    shp_fld, oid_fld, shp_type, SR = fc_info(in_fc)
    a = arcpy.da.FeatureClassToNumPyArray(in_fc, "*", "", SR)
    dt = '<f8'
    b = np.array([tuple(i) for i in a[shp_fld]], dtype=dt)
    coords, dist, n_array = n_near(b, N, ordered=True)  # ---- run n_near ----
    fr_to = coords[:, :(N + 1) * 2]
    frum = fr_to[:, :2]
    twos = fr_to[:, 2:].reshape(-1, N, 2)
    r = []
    for i in range(len(frum)):
        f = frum[i]
        t = twos[i]
        for j in range(len(t)):
            r.append(np.array([f, t[j]]))
    rr = np.array(r)
    r0 = np.array([i[np.lexsort((i[:, 1], i[:, 0]))] for i in rr])  # slicesort
    r1 = r0.reshape(-1, 4)
    r2 = _uniq_by_row_col(r1, axis=0)  # use if np.version < 1.13
    # r2 = unique_2d(r1)
    r3 = r2[np.argsort(r2[..., 0])]
    r3 = r3.reshape(-1, 2, 2)
    if not testing:
        s = []
        for pt in r3:
            arr = arcpy.Array([arcpy.Point(*p) for p in pt])
            s.append(arcpy.Polyline(arr, SR))
        if arcpy.Exists(out_fc):
            arcpy.Delete_management(out_fc)
        arcpy.CopyFeatures_management(s, out_fc)
        return None
    else:
        return a, b, r0, r1, r2, r3
Example #7
0
    def SimulateLandslide(self, landslides_feature, simulated_landslides, mean, stdev):
        arcpy.CreateFeatureclass_management(os.path.dirname(simulated_landslides), os.path.basename(simulated_landslides), "Polygon", landslides_feature)
        arcpy.AddField_management(simulated_landslides, field_name="SIM", field_type="SHORT")
        mean = mean
        stdev = stdev
        searchLandslides = arcpy.SearchCursor(landslides_feature)
        insertLandslide = arcpy.InsertCursor(simulated_landslides)
        pointArray = arcpy.Array()
        newVertex = arcpy.Point()

        geom = arcpy.Describe(landslides_feature).ShapeFieldName
        try:
            for landslide in searchLandslides:
                landslideGeometry = landslide.getValue(geom)
                simulatedLandslide = insertLandslide.newRow()
                i = 0
                for part in landslideGeometry:
                    for vertex in landslideGeometry.getPart(i):
                        if part:
                            newVertex.X = random.normalvariate(mean, stdev) + float(vertex.X)
                            newVertex.Y = random.normalvariate(mean, stdev) + float(vertex.Y)
                            pointArray.add(newVertex)
                        else:
                            arcpy.AddMessage("Internal circle")
                    i = i+1
                    simulatedLandslide.shape = pointArray
                simulatedLandslide.SIM = 1
                insertLandslide.insertRow(simulatedLandslide)
                pointArray.removeAll()
        except:
            arcpy.AddMessage(arcpy.GetMessages(0) + " " + arcpy.GetMessages(1) + " " + arcpy.GetMessages(2))
            pointArray.removeAll()

        newVertex = None
        pointArray = None
        del searchLandslides
        del insertLandslide
Example #8
0
def get_extent_mask(extent, mask):
    """
    Derive the mask for the customised input extent.

    :param extent: `str` the input extent
    :param mask: `file` the output mask
    """

    extent_list = str(extent).split()

    # Array to hold points
    array = arcpy.Array()

    # Create the bounding box
    array.add(arcpy.Point(float(extent_list[0]), float(extent_list[1])))
    array.add(arcpy.Point(float(extent_list[2]), float(extent_list[1])))
    array.add(arcpy.Point(float(extent_list[2]), float(extent_list[3])))
    array.add(arcpy.Point(float(extent_list[0]), float(extent_list[3])))
    array.add(arcpy.Point(float(extent_list[0]), float(extent_list[1])))

    # Create the polygon object
    polygon = arcpy.Polygon(array)
    array.removeAll()
    arcpy.CopyFeatures_management(polygon, mask)
def craetePolygon(coordinates):

    inXmin = float(coordinates[0])
    inYmin = float(coordinates[1])
    inXmax = float(coordinates[2])
    inYmax = float(coordinates[3])

    pntLL = arcpy.Point(inXmin, inYmin)
    pntLR = arcpy.Point(inXmax, inYmin)
    pntUR = arcpy.Point(inXmax, inYmax)
    pntUL = arcpy.Point(inXmin, inYmax)

    array = arcpy.Array()

    # Create the bounding box
    array.add(pntLL)
    array.add(pntLR)
    array.add(pntUR)
    array.add(pntUL)

    #Create the polygon object
    polygon = arcpy.Polygon(array)
    array.removeAll()
    return polygon
Example #10
0
def createIn(_CellRow, _ShanXingInsertCursor, p_lon, p_lat, XiaoQuFields):
    cellPolygon = _ShanXingInsertCursor.newRow()
    #p_lon = CellRow.getValue(lonField)
    #p_lat = CellRow.getValue(latField)
    CirclePerPointAngel = 360.0 / float(CirclePointNum)
    rdloncos = 111 * math.cos(p_lat * rad)
    pointArray = arcpy.Array()
    _angel = 0
    for i in range(CirclePointNum + 1):
        _angel = CirclePerPointAngel * i
        if (_angel == 360):
            _angel = 0
        rslonX = circleR * math.sin(_angel * rad)
        rslatX = circleR * math.cos(_angel * rad)
        lonX = p_lon + (rslonX / rdloncos)
        latX = p_lat + rslatX / 111
        pointX = arcpy.Point(lonX, latX)
        pointArray.add(pointX)
    cellPolygon.shape = pointArray
    for XiaoQuField in XiaoQuFields:
        XQFieldName = XiaoQuField.name
        if (XQFieldName != "OBJECTID"):
            cellPolygon.setValue(XQFieldName, _CellRow.getValue(XQFieldName))
    _ShanXingInsertCursor.insertRow(cellPolygon)
Example #11
0
def calcTriangle(wndDrt, strtLat, strtLong, distance, inputFilename,
                 timestamp):
    bearing1 = ((wndDrt + 180.5) %
                360.0) * 0.0174533  # center of triangle + 0.5 degrees
    bearing2 = ((wndDrt - 180.5) %
                360.0) * 0.0174533  # center of triangle - 0.5 degrees
    airMonitorLoc = arcpy.Point(
        strtLong, strtLat
    )  # defnie the air monitor location as one point of the triangle
    coordP1 = calcCoords(strtLat, strtLong, bearing1,
                         distance)  # create another coordinate point
    upPoint1 = arcpy.Point(coordP1[1], coordP1[0])
    coordP2 = calcCoords(strtLat, strtLong, bearing2,
                         distance)  # create another coordinate point
    upPoint2 = arcpy.Point(coordP2[1], coordP2[0])

    # Create a polygon geometry
    array = arcpy.Array([airMonitorLoc, upPoint1, upPoint2])
    polygon = arcpy.Polygon(array)
    # Open an InsertCursor and insert the new geometry
    cursor = arcpy.da.InsertCursor(inputFilename, ['SHAPE@', WIND_FIELD])
    cursor.insertRow([polygon, timestamp])
    # Delete cursor object
    del cursor
Example #12
0
def create_single_direction_line(insert_cur, center_point, filed_obj,
                                 insert_field, distance_name, direction_name):
    # 插入一条方向线
    # insert_cur:插入记录游标(注意游标生成按insert_field生成)
    # center_point:中心点坐标(起点坐标)
    # filed_obj:属性对象
    # insert_field:插入要素类中对应的属性列表(顺序也对应)
    # distance_name:filed_obj中表示距离的属性(和插入表中的距离字段名称一致)
    # direction_name:filed_obj中表示方向标识的属性(和插入表中的方向字段名称一致)

    direction_num = filed_obj["direction_num"]
    distance = filed_obj[distance_name]
    end_point = calculate_end_point(center_point, distance,
                                    filed_obj[direction_name], direction_num)
    od_polyline = arcpy.Polyline(arcpy.Array([center_point, end_point]))

    insert_row_list = []
    for filed in insert_field:
        if filed == "SHAPE@":
            insert_row_list.append(od_polyline)
        else:
            insert_row_list.append(filed_obj[filed])
    insert_cur.insertRow(insert_row_list)
    return
 def onLine(self, line_geometry):
     array = arcpy.Array()
     part = line_geometry.getPart(0)
     for pt in part:
         array.add(pt)
     array.add(line_geometry.firstPoint)
     polygon = arcpy.Polygon(array)
     if arcpy.Exists("in_memory/polygons"):
         arcpy.Delete_management("in_memory/polygons")
     arcpy.RefreshActiveView()
     arcpy.CopyFeatures_management(polygon, "in_memory/polygons")
     mxd = arcpy.mapping.MapDocument("CURRENT")
     aes = [
         x for x in arcpy.mapping.ListLayers(mxd) if x.name[0:3] == 'AES'
     ][0]
     poligono = [
         x for x in arcpy.mapping.ListLayers(mxd) if x.name == 'polygons'
     ][0]
     arcpy.SelectLayerByLocation_management(ccpp, "INTERSECT", poligono,
                                            "#", "NEW_SELECTION")
     suma = sum([x[0] for x in arcpy.da.SearchCursor(ccpp, ["CANT_EST"])])
     arcpy.RefreshActiveView()
     pythonaddins.MessageBox("Establecimientos Totales: {}".format(suma),
                             "INEI", 0)
Example #14
0
def addMasterBoundary(master_fgdb_path, master_md_name, local_fgdb_MDMasterFC):
#     This is necessary, since the ImportMosaicDatasetGeometry tool won't replace an empty boundary
#     MasterBoundaryFC = RasterConfig.MasterBoundaryFC
    if arcpy.Exists(local_fgdb_MDMasterFC):
        arcpy.AddMessage("MD Master Boundary Feature Class: {0}".format(local_fgdb_MDMasterFC))
        descBound = arcpy.Describe(local_fgdb_MDMasterFC)
        SpatRefBound = descBound.SpatialReference.name
        arcpy.AddMessage("Master Boundary Feature Class has spatial reference: {0}".format(SpatRefBound))
        if SpatRefBound == "WGS_1984_Web_Mercator_Auxiliary_Sphere":
            # record count of the specified boundary feature class should be 1
#             result = arcpy.GetCount_management(local_fgdb_MDMasterFC)
#             Utility.addToolMessages()
#             countRows = int(result.getOutput(0))
#             # @TODO Not sure we need this check
#             if countRows == 1:
            fc = master_fgdb_path + r"\AMD_" + master_md_name + r"_BND"
            arcpy.AddMessage("Master Mosaic Boundary feature class name:             {0}".format(fc))
            fields = ['SHAPE@']
            array = arcpy.Array([arcpy.Point(0, 0),
                    arcpy.Point(1, 0),
                    arcpy.Point(1, 1)])
            polygon = arcpy.Polygon(array)
            with arcpy.da.InsertCursor(fc, fields) as cursor:  # @UndefinedVariable
                cursor.insertRow([polygon])
            del cursor
            # Replace the boundary row (just created) with the row in MasterBoundaryFC
            master_md_path = os.path.join(master_fgdb_path, master_md_name)
            arcpy.AddMessage("Importing boundary to Master GDB...")
            arcpy.ImportMosaicDatasetGeometry_management(master_md_path, target_featureclass_type="BOUNDARY", target_join_field="OBJECTID", input_featureclass=local_fgdb_MDMasterFC, input_join_field="OBJECTID")
            Utility.addToolMessages()
#             else:
#                 arcpy.AddError("\nExiting: {0} should contain only 1 row.".format(local_fgdb_MDMasterFC))
        else:
            arcpy.AddError("Spatial reference of the supplied Master Boundary is not Web Mercator ('{}') Cannot continue.".format(SpatRefBound))
    else:
        arcpy.AddWarning("Master Boundary feature class not found: {0}. Continuing without it".format(local_fgdb_MDMasterFC))
Example #15
0
def polygonsToMultiPolygon(inPolygons):
    arcpy.AddMessage("polygonsToMultiPolygon")
    desc = arcpy.Describe(inPolygons)
    shapeFieldName = desc.ShapeFieldName
    polygonRows = arcpy.SearchCursor(inPolygons)
    multiPoly = (arcpy.CreateFeatureclass_management(
        arcpy.env.scratchGDB, "multiPolygon", "POLYGON", inPolygons,
        "SAME_AS_TEMPLATE", "SAME_AS_TEMPLATE", desc.spatialReference))
    polyArray = arcpy.Array()
    for polygonRow in polygonRows:
        feat = polygonRow.getValue(shapeFieldName)
        i = 0
        while i < feat.partCount:
            polyArray.append(feat.getPart(i))
            i += 1
    del polygonRows
    insertCursor = arcpy.InsertCursor(multiPoly)
    newRow = insertCursor.newRow()
    polygon = arcpy.Polygon(polyArray)
    newRow.shape = polygon
    insertCursor.insertRow(newRow)
    del newRow
    del insertCursor
    return arcpy.env.scratchGDB + "/multiPolygon"
Example #16
0
def create_aoi_mask_layer(paths, aoi_feature_layer, output_feature_class, style_layer=None):
    """Create a visibility mask to focus on an Area of Interest in a map."""
    assert has_arcpy, 'ArcPy is required (environment with arcpy referencing ArcGIS Pro functionality) to create an AOI mask.'

    # get the style layer if one is not provided
    styl_lyr = paths.dir_arcgis_lyrs / 'aoi_mask.lyrx' if style_layer is None else style_layer

    # ensure aoi is polygon
    geom_typ = arcpy.Describe(aoi_feature_layer).shapeType
    assert geom_typ == 'Polygon', 'The area of interest must be a polygon.'

    # if multiple polygons, dissolve into one
    if int(arcpy.management.GetCount(aoi_feature_layer)[0]) > 1:
        aoi_feature_layer = arcpy.analysis.PairwiseDissolve(aoi_feature_layer, arcpy.Geometry())

    # simplify the geometry for rendering efficiency later
    desc = arcpy.Describe(aoi_feature_layer)
    tol_val = (desc.extent.width + desc.extent.height) / 2 * 0.01
    smpl_feat = arcpy.cartography.SimplifyPolygon(aoi_feature_layer, out_feature_class=arcpy.Geometry(),
                                                  algorithm='POINT_REMOVE', tolerance=tol_val,
                                                  collapsed_point_option='NO_KEEP').split(';')[0]

    # create polygon covering the entire globe to cut out from
    coord_lst = [[-180.0, -90.0], [-180.0, 90.0], [180.0, 90.0], [180.0, -90.0], [-180.0, -90.0]]
    coord_arr = arcpy.Array((arcpy.Point(x, y) for x, y in coord_lst))
    mask_geom = [arcpy.Polygon(coord_arr, arcpy.SpatialReference(4326))]

    # erase the simplified area of interest from the global extent polygon
    mask_fc = arcpy.analysis.Erase(mask_geom, smpl_feat, output_feature_class)

    # create a layer and make it pretty
    strt_lyr = arcpy.management.MakeFeatureLayer(mask_fc)[0]
    styl_lyr = str(styl_lyr) if isinstance(styl_lyr, Path) else styl_lyr
    lyr = arcpy.management.ApplySymbologyFromLayer(strt_lyr, styl_lyr)[0]

    return lyr
Example #17
0
    def splitShape(feat, splitDist):
        # Count the number of points in the current multipart feature
        #
        partcount = feat.partCount
        partnum = 0
        # Enter while loop for each part in the feature (if a singlepart feature
        # this will occur only once)
        #
        lineArray = arcpy.Array()

        while partnum < partcount:
            # Print the part number
            #
            #print "Part " + str(partnum) + ":"
            part = feat.getPart(partnum)
            #print part.count

            totalDist = 0

            pnt = part.next()
            pntcount = 0

            prevpoint = None
            shapelist = []

            # Enter while loop for each vertex
            #
            while pnt:

                if not (prevpoint is None):
                    thisDist = distPoint(prevpoint, pnt)
                    maxAdditionalDist = splitDist - totalDist

                    #print thisDist, totalDist, maxAdditionalDist

                    if (totalDist + thisDist) > splitDist:
                        while (totalDist + thisDist) > splitDist:
                            maxAdditionalDist = splitDist - totalDist
                            #print thisDist, totalDist, maxAdditionalDist
                            newpoint = midpoint(prevpoint, pnt,
                                                maxAdditionalDist, thisDist)
                            lineArray.add(newpoint)
                            shapelist.append(lineArray)

                            lineArray = arcpy.Array()
                            lineArray.add(newpoint)
                            prevpoint = newpoint
                            thisDist = distPoint(prevpoint, pnt)
                            totalDist = 0

                        lineArray.add(pnt)
                        totalDist += thisDist
                    else:
                        totalDist += thisDist
                        lineArray.add(pnt)
                        #shapelist.append(lineArray)
                else:
                    lineArray.add(pnt)
                    totalDist = 0

                prevpoint = pnt
                pntcount += 1

                pnt = part.next()

                # If pnt is null, either the part is finished or there is an
                #   interior ring
                #
                if not pnt:
                    pnt = part.next()
                    if pnt:
                        print "Interior Ring:"
            partnum += 1

        if (lineArray.count > 1):
            shapelist.append(lineArray)

        return shapelist
Example #18
0
def to_fc(out_fc, a, b=None, dim=2, flds=['Id', 'X', 'Y'], SR_code=None):
    """Reconstruct a featureclass from a deconstructed pair of arrays.

    This function reverses the functionality of to_array which splits a
    featureclass into an array of geometry and one of attributes.

    One can perform operations on one or the other or both, then reassemble
    into a new file.

    Requires:
    --------
    `out_fc` : filename
        full path and name for the output featureclass
    `a` : geometry array
        the array of geometry objects
    `b` : attribute array
        the array of attributes for 'a'
    `dim` : geometry type
        - 0 (point)
        - 1 (polyline) or
        - 2 (polygon)
    `fld` : id and shape field(s)
        normally ['Id, 'X', 'Y'] if deconstructed using 'to_array'
    `SR_code` :
        The spatial reference code of the output geometry

        - 4326 for GCS WGS84
        - 2951 for MTM 9

    References:
    ----------

        Spatial reference http://spatialreference.org/
    """
    args = [to_fc.__module__, dedent(to_fc.__doc__)]
    msg = "\n...to_fc ... in {} failed\n{}".format(*args)
    try:
        SR = arcpy.SpatialReference(SR_code)
    except ValueError:
        tweet("Spatial reference is in error")
        tweet(msg)
        return None
    if (dim not in (0, 1, 2)) or (len(flds) not in (2, 3)):
        tweet(msg)
        return None
    if len(flds) == 2:
        oid_fld, shp_fld = flds
    else:
        oid_fld, shp_fld = flds[0], flds[1:3]
    if dim == 0:
        arr_pnts(a, out_fc, shp_fld, SR)
#    geom = _split_array(a, fld=oid_fld)
    geom = np.split(a, np.where(np.diff(a[oid_fld]))[0] + 1)
    prts = [i[['Xs', 'Ys']].tolist() for i in geom]
    if dim == 1:
        arr_polyline_fc(geom, out_fc, oid_fld, shp_fld, SR)
    else:
        # arr_polygon_fc(geom, out_fc, oid_fld, shp_fld, SR)
        # pts = _arr_common(a, oid_fld, shp_fld)
        f = []
        for pt in prts:  # g
            f.append(
                arcpy.Polygon(arcpy.Array([arcpy.Point(*p) for p in pt]), SR))
#    _shapes_fc(f, out_fc)
#    arcpy.da.ExtendTable(out_fc, table_match_field=oid_fld,
#                         in_array=b, array_match_field=oid_fld)
    return f
Example #19
0
    def make_trip_shp(self):
        '''line shapes with data on count of trips following each shape, further
        broken out by route, service id and trip direction'''

        data_df = self.agg_to_tripshp()

        fc_linshps = "GTFSLines_{}{}".format(self.agency_formatted,
                                             self.data_year)

        if (arcpy.Exists(fc_linshps)):
            arcpy.Delete_management(fc_linshps)

        #add route file fields
        arcpy.CreateFeatureclass_management(self.workspace, fc_linshps,
                                            "POLYLINE", "", "", "",
                                            self.sacog_projexn)
        arcpy.AddField_management(
            fc_linshps, self.f_shpsrc, "TEXT", "", "",
            20)  # whether shape from shapes.txt or stoptimes.txt
        arcpy.AddField_management(fc_linshps, self.f_shapeid, "TEXT", "", "",
                                  40)
        arcpy.AddField_management(fc_linshps, self.f_agencyname, "TEXT", "",
                                  "", 40)
        arcpy.AddField_management(fc_linshps, self.f_routeid, "TEXT", "", "",
                                  50)
        arcpy.AddField_management(fc_linshps, self.f_routesname, "TEXT", "",
                                  "", 20)
        arcpy.AddField_management(fc_linshps, self.f_routelname, "TEXT", "",
                                  "", 100)
        arcpy.AddField_management(fc_linshps, self.f_svc_id, "TEXT", "", "",
                                  50)
        arcpy.AddField_management(fc_linshps, self.f_tripdir, "TEXT", "", "",
                                  1)
        arcpy.AddField_management(
            fc_linshps,
            self.f_tripcnt_day,
            "SHORT",
        )

        data_records = data_df.to_dict(
            'records')  # [{colA:val1, ColB: val1},{ColA:val2, ColB:val2}...]

        data_fields = [col for col in data_df.columns]

        route_cur = arcpy.da.InsertCursor(
            fc_linshps, [self.f_esri_shape, self.f_shpsrc] + data_fields)

        print("writing rows to feature class {}...".format(fc_linshps))

        df_shapes_aug = self.augment_shpstbl(
        )  # df fields = shape id, lat, long, sequence, shape source

        for row in data_records:

            shape_id = row[self.f_shapeid]
            #put points into order so trip line draws correctly
            thisshape_df = df_shapes_aug[df_shapes_aug[self.f_shapeid] == shape_id] \
                           .sort_values(by = self.f_pt_seq)

            shp_source = list(thisshape_df[self.f_shpsrc])[0]

            lats = list(thisshape_df[self.f_pt_lat])
            lons = list(thisshape_df[self.f_pt_lon])
            array = arcpy.Array()
            pt = arcpy.Point()
            for idx in range(0, len(lats)):
                pt.X = float(lons[idx])
                pt.Y = float(lats[idx])
                array.add(pt)
            polyline = arcpy.Polyline(array, self.spatialref_wgs)
            if self.sacog_projexn != self.spatialref_wgs:
                polyline = polyline.projectAs(self.sacog_projexn)

            data_vals = [
                row[f.name] for f in arcpy.ListFields(fc_linshps)
                if f.name in data_fields
            ]
            row_vals = [polyline, shp_source] + data_vals
            # pdb.set_trace()

            route_cur.insertRow(tuple(row_vals))

        del route_cur
Example #20
0
print arcpy.GetMessages()

Num = "TrackCount"
NumType = "INTEGER"
arcpy.AddField_management(outputName, Num, NumType)
print arcpy.GetMessages()

#3c: reading information from file
f = open(r"Z:\AdvancedGIS\Assignment 7\5Hurricanes.txt", "r")
cursor = arcpy.da.InsertCursor(outputName, ["SHAPE@"])
lines = f.readlines()
i = 0
for line in lines:
    element = line.split(",")
    if (line.startswith("AL")):
        hdata = arcpy.Array()
        index = 0
        wname = element[1]
        while (wname[index] == " "):
            index = index + 1
        name = wname[index:len(wname)]
        print name
        tracks = float(element[2])
        points = 0
        i += 1
        lat = 0
        long = 0
    else:
        if (element[5].endswith("E")):
            coord = element[5]
            long = float(coord[0:len(coord) - 1])
def downloadData():
    print "beginning data download..."
    proxy_port = None
    proxy_url = None

    agolSH = AGOLTokenSecurityHandler(username=username, password=password)

    fl = FeatureLayer(url=feature_service_url,
                      securityHandler=agolSH,
                      proxy_port=proxy_port,
                      proxy_url=proxy_url,
                      initialize=True)

    oid_query_response = fl.query(returnIDsOnly=True)
    oid_list = oid_query_response["objectIds"]
    oid_list.sort()
    list_length = len(oid_list)
    print list_length

    if os.path.exists(output_folder + os.sep + feature_class_name + "_" +
                      the_date + ".gdb"):
        shutil.rmtree(output_folder + os.sep + feature_class_name + "_" +
                      the_date + ".gdb")
    if os.path.isfile(output_folder + os.sep + "Errors.csv"):
        os.remove(output_folder + os.sep + "Errors.csv")
    arcpy.CreateFileGDB_management(output_folder,
                                   feature_class_name + "_" + the_date)
    output_fgdb = output_folder + os.sep + feature_class_name + "_" + the_date + ".gdb"

    def updatedQuery(low, high, trigger):
        if low != high:
            updated_query = """"OBJECTID" >= """ + str(
                low) + " AND " + """"OBJECTID" < """ + str(high)
            if trigger == 1:
                updated_query = """"OBJECTID" >= """ + str(low)
        else:
            updated_query = """"OBJECTID" = """ + str(low)
        return updated_query

    errors = []
    error_fields = []
    fc = ""
    fields = ["SHAPE@"]
    low = 0
    high = 1000
    counter = 0
    while low <= list_length:
        min = oid_list[low]
        try:
            max = oid_list[high]
            trigger = 0
        except:
            totalFixed = list_length - 1
            max = oid_list[totalFixed]
            trigger = 1
        updated_query = updatedQuery(min, max, trigger)
        returned_data = fl.query(where=updated_query,
                                 out_fields='*',
                                 returnGeometry=True)
        returned_data_string = str(returned_data)
        d = json.loads(returned_data_string)
        print "dictionary compiled."

        if counter == 0:
            wkid = d['spatialReference']['latestWkid']
            sr = arcpy.SpatialReference(wkid)
            arcpy.CreateFeatureclass_management(output_fgdb,
                                                feature_class_name, "POLYLINE",
                                                "", "DISABLED", "DISABLED", sr)
            fc = output_fgdb + os.sep + feature_class_name
            for field in d['fields']:
                print field["name"]
                error_fields.append(field["name"])
                if field["name"] != "OBJECTID" and field[
                        "name"] != "Shape_Length" and field[
                            "name"] != "GlobalID":
                    text_length = ""
                    if field["type"] == "esriFieldTypeInteger":
                        type = "LONG"
                    elif field["type"] == "esriFieldTypeSmallInteger":
                        type = "SHORT"
                    elif field["type"] == "esriFieldTypeString":
                        type = "TEXT"
                        text_length = field["length"]
                    elif field["type"] == "esriFieldTypeDouble":
                        type = "DOUBLE"
                    elif field["type"] == "esriFieldTypeFloat":
                        type = "FLOAT"
                    elif field["type"] == "esriFieldTypeDate":
                        type = "DATE"
                    arcpy.AddField_management(fc, field["name"], type, "", "",
                                              text_length, field["alias"])
                    fields.append(field["name"])
            errors.append(error_fields)

        cursor = arcpy.da.InsertCursor(fc, fields)
        records = d["features"]
        for record in records:
            try:
                geom = record["geometry"]
                paths = geom["paths"]
                new_geom = arcpy.Array()
                for part in paths:
                    this_part = arcpy.Array()
                    for point in part:
                        this_point = arcpy.Point(point[0], point[1])
                        this_part.append(this_point)
                    new_geom.append(this_part)
                polyline = arcpy.Polyline(new_geom)
            except:
                polyline = arcpy.Polyline(
                    arcpy.Array(arcpy.Array(arcpy.Point(0, 0))))
                error_record = []
                for err_fld in error_fields:
                    error_record.append(record["attributes"][err_fld])
                errors.append(error_record)
                print record

            values = [polyline]
            attributes = record["attributes"]
            for field in fields:
                if field != "SHAPE@":
                    values.append(attributes[field])

            cursor.insertRow(values)
            counter += 1
            print str(counter) + "\\" + str(list_length)

        low += 1000
        high += 1000

    no_geom_csv = open(output_folder + os.sep + "Errors.csv", 'wb')
    writer = csv.writer(no_geom_csv)
    writer.writerows(errors)
    no_geom_csv.close()
Example #22
0
def main(in_file):
    # Script: input = full path to GDB
    homeDir = path.dirname(in_file)
    if not homeDir:
        # Toolbox: input = layer name only
        homeDir = env.workspace
        in_name = in_file
    else:
        in_name = path.basename(in_file)
    out_name = 'tr' + in_name[1:]
    if arcpy.Exists(out_name):
        out_name = f_utils.uniqName(out_name)
    temp_name = path.basename(out_name)

    template = ""
    has_m = 'DISABLED'  #'ENABLED'
    has_z = 'DISABLED'
    geometry_type = 'POLYLINE'

    try:
        # pass in the name only
        line_FC = arcpy.CreateFeatureclass_management('in_memory', temp_name,
                                                      geometry_type, template,
                                                      has_m, has_z, sr)
        # template doesn't work? Field list for insert cursor
        f_names = []
        # First add shape
        f_names.append("SHAPE@")
        # add fields from library
        for f_name, f_type in f_utils.fieldLists.pathCols:
            arcpy.AddField_management(line_FC, f_name, f_type)
            f_names.append(f_name)
        # open an insert cursor on in_memory\FC
        with arcpy.da.InsertCursor(line_FC, f_names) as iCur:
            # open a search cursor on input layer
            sqlClause = (None, 'ORDER BY "individual", "timevalue"')
            with arcpy.da.SearchCursor(in_file,
                                       f_utils.fieldLists.pathSource,
                                       None,
                                       sr,
                                       False,
                                       sql_clause=sqlClause) as sCur:
                points = arcpy.Array()
                # get first row
                row = sCur.next()
                # store deploy values
                from_id, from_date, from_loc, indID, devID, ptt = row[1:7]
                # add first point to array
                points.add(arcpy.Point(*row[0]))
                # iterate over remaining rows
                for row in sCur:
                    # trap for grouped tags
                    if row[4] <> indID:
                        # store deploy values
                        from_id, from_date, from_loc, indID, devID, ptt = row[
                            1:7]
                        # flush array
                        points = arcpy.Array()
                        # add first point to new array
                        points.add(arcpy.Point(*row[0]))
                        continue
                    # add next point
                    points.add(arcpy.Point(*row[0]))
                    # construct a path using the two points in the array
                    segment = arcpy.Polyline(points, sr, False, False)
                    # update values for new row
                    to_id, to_date, to_loc = row[1:4]
                    dist = calcVincentyInverse(points[0].Y, points[0].X,
                                               points[1].Y,
                                               points[1].X)[0] / 1000.0
                    delta = to_date - from_date
                    # convert to hours
                    elapsed = (delta.days * 24) + (delta.seconds / 3600.0)
                    # trap for indentical timevals
                    if elapsed <= 0:
                        elapsed = 0.01666667
                    speed = dist / elapsed  # speed = dist/elapsed if elapsed > 0 else 0.0
                    # add path to feature class
                    iCur.insertRow([
                        segment, from_id, to_id, from_date, to_date, from_loc,
                        to_loc, indID, devID, ptt, dist, elapsed, speed
                    ])
                    # remove "from" point
                    points.remove(0)
                    from_id = to_id
                    from_date = to_date
                    from_loc = to_loc

    # Return geoprocessing specific errors
    except arcpy.ExecuteError:
        msgs = arcpy.GetMessages(2)
        arcpy.AddError(msgs)
        print msgs
    except:
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]
        # Concatenate information together concerning the error into a message string
        pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(
            sys.exc_info()[1])
        msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages(2) + "\n"
        # Return python error messages for use in script tool or Python Window
        arcpy.AddError(pymsg)
        arcpy.AddError(msgs)
        # Print Python error messages for use in Python / Python Window
        print pymsg + "\n"
        print msgs
    finally:
        arcpy.AddMessage('Finished with Path Creation')

    # persist to gdb
    out_file = arcpy.CopyFeatures_management(line_FC, out_name)
    return (out_file)
                                       pointGeometry.centroid.Y) /
                                      (pointGeometryNext.centroid.X -
                                       pointGeometry.centroid.X))
                    AngleDegree = Angle * 180.0 / math.pi
                else:
                    Angle = 0
                print(AngleDegree)
                # Construct Perpendicular line
                PerpendicularPoint1 = pointGeometry.pointFromAngleAndDistance(
                    180 - AngleDegree,
                    float(BufferSize) + 1.0)
                PerpendicularPoint2 = pointGeometry.pointFromAngleAndDistance(
                    180 - AngleDegree, -float(BufferSize) - 1.0)
                LineGeometry = arcpy.Polyline(
                    arcpy.Array([
                        PerpendicularPoint1.centroid, pointGeometry.centroid,
                        PerpendicularPoint2.centroid
                    ]))
                LineGeometryList.append(LineGeometry)
                Position = float(Position) + float(Step)

    arcpy.CopyFeatures_management(
        LineGeometryList, os.path.join(arcpy.env.scratchGDB, "PerpenResult"))

    # Line Buffer
    arcpy.Buffer_analysis(in_features=lineFC,
                          out_feature_class=os.path.join(
                              arcpy.env.scratchGDB, "buffer"),
                          buffer_distance_or_field=BufferSize,
                          line_side="FULL",
                          line_end_type="ROUND",
                          dissolve_option="ALL",
Example #24
0
        arcpy.AddField_management(line_FC, f_name, f_type)
        f_names.append(f_name)

    # open an insert cursor on the line class
    with arcpy.da.InsertCursor(line_FC, f_names) as iCur:
        # list user fields
        f_names = ['SHAPE@XY', tag_field, time_field]
        for f_name, f_type in in_cols:
            f_names.append(str(f_name))
        f_count = len(f_names)

        sql_fields = "ORDER BY {0},{1}".format(tag_field, time_field)
        sql_clause = (None, sql_fields)
        with arcpy.da.SearchCursor(in_file, f_names, None, sr, False,
                                   sql_clause) as sCur:
            points = arcpy.Array()
            # Start of file
            row = sCur.next()
            ptt = int(row[1])
            from_date = row[2]
            from_loc = util.SerDate(from_date)
            point = arcpy.Point(*row[0])
            point.M = from_loc
            points.add(point)
            # iterate over remaining rows
            curr_tag = ptt
            for row in sCur:
                ptt = int(row[1])
                if ptt != curr_tag:  # new tag
                    ptt = int(row[1])
                    from_date = row[2]
Example #25
0
    for df in df_lst:
        arrangeDFs(pageLayoutRow, df.name)
        # Only creates geometry for data frames on the page.
        if (df.elementPositionX > 0 and df.elementPositionX < 11 and 
            df.elementPositionY > 0 and df.elementPositionY < 8.5):
            XMin = df.extent.XMin 
            YMin = df.extent.YMin 
            XMax = df.extent.XMax 
            YMax = df.extent.YMax 
            # A list of features and coordinate pairs
            df_info = [[XMin, YMin],[XMax, YMin],[XMax, YMax],[XMin, YMax]]
            feature_info.append(df_info)
    
    # A list that will hold each of the Polygon objects
    features = []
    
    for feature in feature_info:
        # Create a Polygon object based on the array of points
        # Append to the list of Polygon objects
        features.append(
            arcpy.Polygon(
                arcpy.Array([arcpy.Point(*coords) for coords in feature])))
    
# Persist a copy of the Polygon objects using CopyFeatures
outDir = r"P:\15045 - ED Redistribution - Event Specific\R2015\21-Electoral_Boundaries_Commission_Support_Doc\WBS 8 - Geography\James\DataFrame_Polygon_Boxes"
poly_filename = outDir + r"/dataframePolygons" + orient + inset + ".shp"
if os.path.exists(poly_filename):
	os.remove(poly_filename)
arcpy.CopyFeatures_management(features, poly_filename)

del coords, feature_info, features, feature, poly_filename, outDir, mxd, df_lst, df_info, df, inset, XMax, XMin, YMax, YMin, page, orient, ddp, pageName, pageLayoutCursor, pageLayoutTable, pageLayoutRow 
Example #26
0
arcpy.da.NumPyArrayToTable(inarray, temptab)
arcpy.MakeXYEventLayer_management(temptab, "xfield", "yfield", templayer,
                                  spatialref)
points = arcpy.CopyFeatures_management(templayer, pointspath)

arcpy.Delete_management(temptab)
arcpy.Delete_management(templayer)

# Create a Polygon object based on the array of points
# Append to the list of Polygon objects
polyFeatures = []
for i in feature_info:
    pointSet = []
    for j in range(len(i)):
        pointSet.append(arcpy.Point(X=i[j][0], Y=i[j][1]))
    polyFeatures.append(arcpy.Polygon(arcpy.Array(pointSet), spatialref))
polygons = arcpy.CopyFeatures_management(polyFeatures, fileplace)

# Create Join Field, adjust OBJECTID to align with FID (starts at 0)
arcpy.AddField_management(polygons, "joinID", "LONG", 9, "", "", "joinID",
                          "NULLABLE")

desc = arcpy.Describe(resultstable)
arcpy.AddMessage(desc.dataType)
arcpy.AddMessage(desc.extension)
joinNum = arcpy.Describe(resultstable).OIDFieldName

with arcpy.da.UpdateCursor(
        polygons, ["joinID", arcpy.Describe(polygons).OIDFieldName]) as cursor:

    for row in cursor:
Example #27
0
def locations_add_links(logger, the_scenario, modal_layer_name,
                        max_artificial_link_distance_miles):

    # ADD LINKS LOGIC
    # first we near the mode to the locations fc
    # then we iterate through the near table and build up a dictionary of links and all the near XYs on that link.
    # then we split the links on the mode (except pipeline) and preserve the data of that link.
    # then we near the locations to the nodes on the now split links.
    # we ignore locations with near dist == 0 on those nodes.
    # then we add the artificial link and note which locations got links.
    # then we set the connects_to  field if the location was connected.

    logger.debug(
        "start: locations_add_links for mode: {}".format(modal_layer_name))

    scenario_gdb = the_scenario.main_gdb
    fp_to_modal_layer = os.path.join(scenario_gdb, "network", modal_layer_name)

    locations_fc = the_scenario.locations_fc
    arcpy.DeleteField_management(fp_to_modal_layer, "LOCATION_ID")
    arcpy.AddField_management(os.path.join(scenario_gdb, modal_layer_name),
                              "LOCATION_ID", "long")

    arcpy.DeleteField_management(fp_to_modal_layer, "LOCATION_ID_NAME")
    arcpy.AddField_management(os.path.join(scenario_gdb, modal_layer_name),
                              "LOCATION_ID_NAME", "text")

    if float(max_artificial_link_distance_miles.strip(" Miles")) < 0.0000001:
        logger.warning(
            "Note: ignoring mode {}. User specified artificial link distance of {}"
            .format(modal_layer_name, max_artificial_link_distance_miles))
        logger.debug(
            "Setting the definition query to artificial = 99999, so we get an empty dataset for the "
            "make_feature_layer and subsequent near analysis")

        definition_query = "Artificial = 999999"  # something to return an empty set
    else:
        definition_query = "Artificial = 0"  # the normal def query.

    if "pipeline" in modal_layer_name:

        if arcpy.Exists(
                os.path.join(scenario_gdb, "network",
                             fp_to_modal_layer + "_points")):
            arcpy.Delete_management(
                os.path.join(scenario_gdb, "network",
                             fp_to_modal_layer + "_points"))

        # limit near to end points
        arcpy.FeatureVerticesToPoints_management(
            in_features=fp_to_modal_layer,
            out_feature_class=fp_to_modal_layer + "_points",
            point_location="BOTH_ENDS")
        logger.debug("start:  make_featurelayer_management")
        arcpy.MakeFeatureLayer_management(fp_to_modal_layer + "_points",
                                          "modal_lyr_" + modal_layer_name,
                                          definition_query)

    else:
        logger.debug("start:  make_featurelayer_management")
        arcpy.MakeFeatureLayer_management(fp_to_modal_layer,
                                          "modal_lyr_" + modal_layer_name,
                                          definition_query)

    logger.debug(
        "adding links between locations_fc and mode {} with max dist of {}".
        format(modal_layer_name, max_artificial_link_distance_miles))

    if arcpy.Exists(os.path.join(scenario_gdb, "tmp_near")):
        logger.debug("start:  delete tmp near")
        arcpy.Delete_management(os.path.join(scenario_gdb, "tmp_near"))

    logger.debug("start:  generate_near")
    arcpy.GenerateNearTable_analysis(locations_fc,
                                     "modal_lyr_" + modal_layer_name,
                                     os.path.join(scenario_gdb, "tmp_near"),
                                     max_artificial_link_distance_miles,
                                     "LOCATION", "NO_ANGLE", "CLOSEST")

    edit = arcpy.da.Editor(os.path.join(scenario_gdb))
    edit.startEditing(False, False)
    edit.startOperation()

    id_fieldname = arcpy.Describe(os.path.join(scenario_gdb,
                                               modal_layer_name)).OIDFieldName

    seenids = {}

    # SPLIT LINKS LOGIC
    # 1) first search through the tmp_near fc and add points from the near on that link.
    # 2) next we query the mode layer and get the mode specific data using the near FID.
    # 3) then we split the old link, and use insert cursor to populate mode specific data into fc for the two new links.
    # 4) then we delete the old unsplit link
    logger.debug("start:  split links")
    with arcpy.da.SearchCursor(
            os.path.join(scenario_gdb, "tmp_near"),
        ["NEAR_FID", "NEAR_X", "NEAR_Y", "NEAR_DIST"]) as scursor:

        for row in scursor:

            # if the near distance is 0, then its connected and we don't need to
            # split the line.
            if row[3] == 0:
                # only give debug warnring if not pipeline.
                if "pipleine" not in modal_layer_name:
                    logger.warning(
                        "Split links code: LOCATION MIGHT BE ON THE NETWORK. Ignoring NEAR_FID {} with NEAR_DIST {}"
                        .format(row[0], row[3]))

            if not row[3] == 0:

                # STEP 1: point geoms where to split from the near XY
                # ---------------------------------------------------
                # get the line ID to split
                theIdToGet = str(row[0])  # this is the link id we need.

                if not theIdToGet in seenids:
                    seenids[theIdToGet] = []

                point = arcpy.Point()
                point.X = float(row[1])
                point.Y = float(row[2])
                point_geom = arcpy.PointGeometry(point,
                                                 ftot_supporting_gis.LCC_PROJ)
                seenids[theIdToGet].append(point_geom)

        # STEP 2 -- get mode specific data from the link
        # ------------------------------------------------
        if 'pipeline' not in modal_layer_name:

            for theIdToGet in seenids:

                # initialize the variables so we dont get any gremlins.
                in_line = None  # the shape geometry
                in_capacity = None  # road + rail
                in_volume = None  # road + rail
                in_vcr = None  # road + rail | volume to capacity ratio
                in_fclass = None  # road | fclass
                in_speed = None  # road | rounded speed
                in_stracnet = None  # rail
                in_density_code = None  # rail
                in_tot_up_dwn = None  # water

                if modal_layer_name == 'road':
                    for row in arcpy.da.SearchCursor(
                            os.path.join(scenario_gdb, modal_layer_name), [
                                "SHAPE@", "Capacity", "Volume", "VCR",
                                "FCLASS", "ROUNDED_SPEED"
                            ],
                            where_clause=id_fieldname + " = " + theIdToGet):
                        in_line = row[0]
                        in_capacity = row[1]
                        in_volume = row[2]
                        in_vcr = row[3]
                        in_fclass = row[4]
                        in_speed = row[5]

                if modal_layer_name == 'rail':
                    for row in arcpy.da.SearchCursor(
                            os.path.join(scenario_gdb, modal_layer_name), [
                                "SHAPE@", "Capacity", "Volume", "VCR",
                                "STRACNET", "DENSITY_CODE"
                            ],
                            where_clause=id_fieldname + " = " + theIdToGet):
                        in_line = row[0]
                        in_capacity = row[1]
                        in_volume = row[2]
                        in_vcr = row[3]
                        in_stracnet = row[4]
                        in_density_code = row[5]

                if modal_layer_name == 'water':
                    for row in arcpy.da.SearchCursor(
                            os.path.join(scenario_gdb, modal_layer_name),
                        ["SHAPE@", "Capacity", "Volume", "VCR", "TOT_UP_DWN"],
                            where_clause=id_fieldname + " = " + theIdToGet):
                        in_line = row[0]
                        in_capacity = row[1]
                        in_volume = row[2]
                        in_vcr = row[3]
                        in_tot_up_dwn = row[4]

                # STEP 3: Split and populate with mode specific data from old link
                # ------------------------------------------------------------------
                split_lines = arcpy.management.SplitLineAtPoint(
                    in_line, seenids[theIdToGet], arcpy.Geometry(), 1)

                if not len(split_lines) == 1:

                    # ROAD
                    if modal_layer_name == 'road':

                        icursor = arcpy.da.InsertCursor(
                            os.path.join(scenario_gdb, modal_layer_name), [
                                'SHAPE@', 'Artificial', 'MODE_TYPE', 'MILES',
                                'FCLASS', 'ROUNDED_SPEED', 'Volume',
                                'Capacity', 'VCR'
                            ])

                        # Insert new links that include the mode-specific attributes
                        for new_line in split_lines:
                            len_in_miles = Q_(new_line.length,
                                              "meters").to("miles").magnitude
                            icursor.insertRow([
                                new_line, 0, modal_layer_name, len_in_miles,
                                in_fclass, in_speed, in_volume, in_capacity,
                                in_vcr
                            ])

                        # Delete cursor object
                        del icursor

                    elif modal_layer_name == 'rail':
                        icursor = arcpy.da.InsertCursor(
                            os.path.join(scenario_gdb, modal_layer_name), [
                                'SHAPE@', 'Artificial', 'MODE_TYPE', 'MILES',
                                'STRACNET', 'DENSITY_CODE', 'Volume',
                                'Capacity', 'VCR'
                            ])

                        # Insert new rows that include the mode-specific attributes
                        for new_line in split_lines:
                            len_in_miles = Q_(new_line.length,
                                              "meters").to("miles").magnitude
                            icursor.insertRow([
                                new_line, 0, modal_layer_name, len_in_miles,
                                in_stracnet, in_density_code, in_volume,
                                in_capacity, in_vcr
                            ])

                        # Delete cursor object
                        del icursor

                    elif modal_layer_name == 'water':

                        icursor = arcpy.da.InsertCursor(
                            os.path.join(scenario_gdb, modal_layer_name), [
                                'SHAPE@', 'Artificial', 'MODE_TYPE', 'MILES',
                                'TOT_UP_DWN', 'Volume', 'Capacity', 'VCR'
                            ])

                        # Insert new rows that include the mode-specific attributes
                        for new_line in split_lines:
                            len_in_miles = Q_(new_line.length,
                                              "meters").to("miles").magnitude
                            icursor.insertRow([
                                new_line, 0, modal_layer_name, len_in_miles,
                                in_tot_up_dwn, in_volume, in_capacity, in_vcr
                            ])

                        # Delete cursor object
                        del icursor

                    else:
                        logger.warning(
                            "Modal_layer_name: {} is not supported.".format(
                                modal_layer_name))

                    # STEP 4:  Delete old unsplit data
                    with arcpy.da.UpdateCursor(os.path.join(
                            scenario_gdb, modal_layer_name), ['OID@'],
                                               where_clause=id_fieldname +
                                               " = " + theIdToGet) as ucursor:
                        for row in ucursor:
                            ucursor.deleteRow()

                # if the split doesn't work.
                else:
                    logger.detailed_debug(
                        "the line split didn't work for ID: {}. "
                        "Might want to investigate. "
                        "Could just be an artifact from the near result being the end of a line."
                        .format(theIdToGet))

    edit.stopOperation()
    edit.stopEditing(True)

    # delete the old features
    # ------------------------
    logger.debug(
        "start:  delete old features (tmp_near, tmp_near_2, tmp_nodes)")
    if arcpy.Exists(os.path.join(scenario_gdb, "tmp_near")):
        arcpy.Delete_management(os.path.join(scenario_gdb, "tmp_near"))

    if arcpy.Exists(os.path.join(scenario_gdb, "tmp_near_2")):
        arcpy.Delete_management(os.path.join(scenario_gdb, "tmp_near_2"))

    if arcpy.Exists(os.path.join(scenario_gdb, "tmp_nodes")):
        arcpy.Delete_management(os.path.join(scenario_gdb, "tmp_nodes"))

    # Add artificial links now.
    # now that the lines have been split add lines from the from points to the nearest node
    # --------------------------------------------------------------------------------------
    logger.debug(
        "start:  add artificial links now w/ definition_query: {}".format(
            definition_query))
    logger.debug("start:  make_featurelayer 2")
    fp_to_modal_layer = os.path.join(scenario_gdb, "network", modal_layer_name)
    arcpy.MakeFeatureLayer_management(fp_to_modal_layer,
                                      "modal_lyr_" + modal_layer_name + "2",
                                      definition_query)
    logger.debug("start:  feature vertices to points 2")
    arcpy.FeatureVerticesToPoints_management(
        in_features="modal_lyr_" + modal_layer_name + "2",
        out_feature_class=os.path.join(scenario_gdb, "tmp_nodes"),
        point_location="BOTH_ENDS")
    logger.debug("start:  generate near table 2")
    arcpy.GenerateNearTable_analysis(locations_fc,
                                     os.path.join(scenario_gdb, "tmp_nodes"),
                                     os.path.join(scenario_gdb, "tmp_near_2"),
                                     max_artificial_link_distance_miles,
                                     "LOCATION", "NO_ANGLE", "CLOSEST")

    logger.debug("start:  delete tmp_nodes")
    arcpy.Delete_management(os.path.join(scenario_gdb, "tmp_nodes"))

    logger.debug("start:  start editor")
    edit = arcpy.da.Editor(os.path.join(scenario_gdb))
    edit.startEditing(False, False)
    edit.startOperation()

    icursor = arcpy.da.InsertCursor(
        os.path.join(scenario_gdb, modal_layer_name), [
            'SHAPE@', 'Artificial', 'MODE_TYPE', 'MILES', 'LOCATION_ID',
            'LOCATION_ID_NAME'
        ])  # add location_id for setting flow restrictions

    location_id_name_dict = get_location_id_name_dict(the_scenario, logger)
    connected_location_ids = []
    connected_location_id_names = []
    logger.debug("start:  search cursor on tmp_near_2")
    with arcpy.da.SearchCursor(
            os.path.join(scenario_gdb, "tmp_near_2"),
        ["FROM_X", "FROM_Y", "NEAR_X", "NEAR_Y", "NEAR_DIST", "IN_FID"
         ]) as scursor:

        for row in scursor:

            if not row[4] == 0:

                # use the unique objectid (in_fid) from the near to determine
                # if we have an in or an out location.
                # then set the flow restrictions appropriately.

                in_fid = row[5]
                location_id_name = location_id_name_dict[in_fid]
                location_id = location_id_name.split("_")[0]
                connected_location_ids.append(location_id)
                connected_location_id_names.append(location_id_name)

                coordList = []
                coordList.append(arcpy.Point(row[0], row[1]))
                coordList.append(arcpy.Point(row[2], row[3]))
                polyline = arcpy.Polyline(arcpy.Array(coordList))

                len_in_miles = Q_(polyline.length,
                                  "meters").to("miles").magnitude

                # insert artificial link attributes
                icursor.insertRow([
                    polyline, 1, modal_layer_name, len_in_miles, location_id,
                    location_id_name
                ])

            else:
                logger.warning(
                    "Artificial Link code: Ignoring NEAR_FID {} with NEAR_DIST {}"
                    .format(row[0], row[4]))

    del icursor
    logger.debug("start:  stop editing")
    edit.stopOperation()
    edit.stopEditing(True)

    arcpy.Delete_management(os.path.join(scenario_gdb, "tmp_near_2"))

    # ALSO SET CONNECTS_X FIELD IN POINT LAYER
    # -----------------------------------------
    logger.debug("start:  connect_x")
    arcpy.AddField_management(os.path.join(scenario_gdb, locations_fc),
                              "connects_" + modal_layer_name, "SHORT")
    arcpy.CalculateField_management(os.path.join(scenario_gdb, locations_fc),
                                    "connects_" + modal_layer_name, 0,
                                    "PYTHON_9.3")

    edit = arcpy.da.Editor(scenario_gdb)
    edit.startEditing(False, False)
    edit.startOperation()
    with arcpy.da.UpdateCursor(
            os.path.join(scenario_gdb, locations_fc),
        ["LOCATION_ID_NAME", "connects_" + modal_layer_name]) as cursor:

        for row in cursor:

            if row[0] in connected_location_id_names:
                row[1] = 1
                cursor.updateRow(row)

    edit.stopOperation()
    edit.stopEditing(True)

    logger.debug("finish: locations_add_links")
Example #28
0
def RotateFeatureClass(inputFC, outputFC,
                       angle=0, pivot_point=None):
    """Rotate Feature Class

    inputFC     Input features
    outputFC    Output feature class
    angle       Angle to rotate, in degrees
    pivot_point X,Y coordinates (as space-separated string)
                Default is lower-left of inputFC

    As the output feature class no longer has a "real" xy locations,
    after rotation, it no coordinate system defined.
    """

    def RotateXY(x, y, xc=0, yc=0, angle=0, units="DEGREES"):
        """Rotate an xy cooordinate about a specified origin

        x,y      xy coordinates
        xc,yc   center of rotation
        angle   angle
        units    "DEGREES" (default) or "RADIANS"
        """
        import math
        x = x - xc
        y = y - yc
        # make angle clockwise (like Rotate_management)
        angle = angle * -1
        if units == "DEGREES":
            angle = math.radians(angle)
        xr = (x * math.cos(angle)) - (y * math.sin(angle)) + xc
        yr = (x * math.sin(angle)) + (y * math.cos(angle)) + yc
        return xr, yr

    # temp names for cleanup
    env_file = None
    lyrFC, lyrTmp, lyrOut   = [None] * 3  # layers
    tmpFC  = None # temp dataset
    Row, Rows, oRow, oRows = [None] * 4 # cursors

    try:
        # process parameters
        try:
            xcen, ycen = [float(xy) for xy in pivot_point.split()]
            pivot_point = xcen, ycen
        except:
            # if pivot point was not specified, get it from
            # the lower-left corner of the feature class
            ext = arcpy.Describe(inputFC).extent
            xcen, ycen  = ext.XMin, ext.YMin
            pivot_point = xcen, ycen

        angle = float(angle)

        # set up environment
        env_file = arcpy.CreateScratchName("xxenv",".xml","file",
                                           os.environ["TEMP"])
        arcpy.SaveSettings(env_file)

        # Disable any GP environment clips or project on the fly
        arcpy.ClearEnvironment("extent")
        arcpy.ClearEnvironment("outputCoordinateSystem")
        WKS = env.workspace
        if not WKS:
            if os.path.dirname(outputFC):
                WKS = os.path.dirname(outputFC)
            else:
                WKS = os.path.dirname(
                    arcpy.Describe(inputFC).catalogPath)
        env.workspace = env.scratchWorkspace = WKS

        # Disable GP environment clips or project on the fly
        arcpy.ClearEnvironment("extent")
        arcpy.ClearEnvironment("outputCoordinateSystem")

        # get feature class properties
        lyrFC = "lyrFC"
        arcpy.MakeFeatureLayer_management(inputFC, lyrFC)
        dFC = arcpy.Describe(lyrFC)
        shpField = dFC.shapeFieldName
        shpType = dFC.shapeType
        FID = dFC.OIDFieldName

        # create temp feature class
        tmpFC = arcpy.CreateScratchName("xxfc","","featureclass")
        arcpy.CreateFeatureclass_management(os.path.dirname(tmpFC),
                                            os.path.basename(tmpFC),
                                            shpType)
        lyrTmp = "lyrTmp"
        arcpy.MakeFeatureLayer_management(tmpFC, lyrTmp)

        # set up id field (used to join later)
        TFID = "XXXX_FID"
        arcpy.AddField_management(lyrTmp, TFID, "LONG")
        arcpy.DeleteField_management(lyrTmp, "ID")

        # rotate the feature class coordinates
        # only points, polylines, and polygons are supported

        # open read and write cursors
        Rows = arcpy.SearchCursor(lyrFC, "", "",
                                  "%s;%s" % (shpField,FID))
        oRows = arcpy.InsertCursor(lyrTmp)
        if shpType  == "Point":
            for Row in Rows:
                shp = Row.getValue(shpField)
                pnt = shp.getPart()
                pnt.X, pnt.Y = RotateXY(pnt.X,pnt.Y,xcen,ycen,angle)
                oRow = oRows.newRow()
                oRow.setValue(shpField, pnt)
                oRow.setValue(TFID,Row.getValue(FID))
                oRows.insertRow(oRow)
        elif shpType in ["Polyline","Polygon"]:
            parts = arcpy.Array()
            rings = arcpy.Array()
            ring = arcpy.Array()
            for Row in Rows:
                shp = Row.getValue(shpField)
                p = 0
                for part in shp:
                    for pnt in part:
                        if pnt:
                            x, y = RotateXY(pnt.X, pnt.Y, xcen, ycen, angle)
                            ring.add(arcpy.Point(x, y, pnt.ID))
                        else:
                            # if we have a ring, save it
                            if len(ring) > 0:
                                rings.add(ring)
                                ring.removeAll()
                    # we have our last ring, add it
                    rings.add(ring)
                    ring.removeAll()
                    # if only one, remove nesting
                    if len(rings) == 1: rings = rings.getObject(0)
                    parts.add(rings)
                    rings.removeAll()
                    p += 1

                # if only one, remove nesting
                if len(parts) == 1: parts = parts.getObject(0)
                if dFC.shapeType == "Polyline":
                    shp = arcpy.Polyline(parts)
                else:
                    shp = arcpy.Polygon(parts)
                parts.removeAll()
                oRow = oRows.newRow()
                oRow.setValue(shpField, shp)
                oRow.setValue(TFID,Row.getValue(FID))
                oRows.insertRow(oRow)
        else:
            #raise Exception, "Shape type {0} is not supported".format(shpType) #UPDATE
            raise Exception("Shape type {0} is not supported".format(shpType))

        del oRow, oRows # close write cursor (ensure buffer written)
        oRow, oRows = None, None # restore variables for cleanup

        # join attributes, and copy to output
        arcpy.AddJoin_management(lyrTmp, TFID, lyrFC, FID)
        env.qualifiedFieldNames = False
        arcpy.Merge_management(lyrTmp, outputFC)
        lyrOut = "lyrOut"
        arcpy.MakeFeatureLayer_management(outputFC, lyrOut)
        # drop temp fields 2,3 (TFID, FID)
        fnames = [f.name for f in arcpy.ListFields(lyrOut)]
        dropList = ";".join(fnames[2:4])
        arcpy.DeleteField_management(lyrOut, dropList)

    #except MsgError, xmsg: #UPDATE
    except MsgError as xmsg:
        arcpy.AddError(str(xmsg))
    except arcpy.ExecuteError:
        tbinfo = traceback.format_tb(sys.exc_info()[2])[0]
        arcpy.AddError(tbinfo.strip())
        arcpy.AddError(arcpy.GetMessages())
        numMsg = arcpy.GetMessageCount()
        for i in range(0, numMsg):
            arcpy.AddReturnMessage(i)
    #except Exception, xmsg: #UPDATE
    except Exception as xmsg:
        tbinfo = traceback.format_tb(sys.exc_info()[2])[0]
        arcpy.AddError(tbinfo + str(xmsg))
    finally:
        # reset environment
        if env_file: arcpy.LoadSettings(env_file)
        # Clean up temp files
        for f in [lyrFC, lyrTmp, lyrOut, tmpFC, env_file]:
            try:
                if f: arcpy.Delete_management(f)
            except:
                pass
        # delete cursors
        try:
            for c in [Row, Rows, oRow, oRows]: del c
        except:
            pass

        # return pivot point
        try:
            pivot_point = "{0} {1}".format(*pivot_point)
        except:
            pivot_point = None

        return pivot_point
def make_GTFS_lines_from_Shapes(shape, route=None):

    if route:
        # Retrieve route info for final output file.
        ShapeRoute = shape + "_" + route
        agency_id = RouteDict[route][0]
        route_short_name = RouteDict[route][1]
        route_long_name = RouteDict[route][2]
        if RouteDict[route][3]:
            route_desc = RouteDict[route][3][:max_route_desc_length]
        else:
            route_desc = ""
        route_type = RouteDict[route][4]
        route_type_text = RouteDict[route][8]
        route_url = RouteDict[route][5]

        route_color = RouteDict[route][6]
        if route_color:
            if ProductName == "ArcGISPro":
                route_color_formatted = "#" + RouteDict[route][6]
            else:
                route_color_formatted = rgb(RouteDict[route][6])
        else:
            route_color_formatted = ""

        route_text_color = RouteDict[route][7]
        if route_text_color:
            if ProductName == "ArcGISPro":
                route_text_color_formatted = "#" + RouteDict[route][7]
            else:
                route_text_color_formatted = rgb(RouteDict[route][7])
        else:
            route_text_color_formatted = ""

    else:
        # Couldn't get route info for this shape
        ShapeRoute = shape
        agency_id = ""
        route_short_name = ""
        route_long_name = ""
        route_desc = ""
        route_type = 0
        route_type_text = ""
        route_url = ""
        route_color = ""
        route_color_formatted = ""
        route_text_color = ""
        route_text_color_formatted = ""

    # Fetch the shape info to create the polyline feature.
    pointsinshapefetch = '''
        SELECT shape_pt_lat, shape_pt_lon, shape_pt_sequence,
        shape_dist_traveled FROM shapes WHERE shape_id='%s'
        ;''' % shape
    c.execute(pointsinshapefetch)
    points = c.fetchall()
    # Sort by sequence
    points.sort(key=operator.itemgetter(2))

    # Create the polyline feature from the sequence of points
    array = arcpy.Array()
    pt = arcpy.Point()
    for point in points:
        pt.X = float(point[1])
        pt.Y = float(point[0])
        array.add(pt)
    polyline = arcpy.Polyline(array)

    # Add the polyline feature to the output feature class
    if ArcVersion == "10.0":
        if ".shp" in OutShapesFCname:
            row = StopsCursor.newRow()
            row.shape = polyline
            row.setValue("RtShpName", ShapeRoute)
            row.setValue("shape_id", shape)
            row.setValue("route_id", route)
            row.setValue("agency_id", agency_id)
            row.setValue("rt_shrt_nm", route_short_name)
            row.setValue("rt_long_nm", route_long_name)
            row.setValue("route_desc", route_desc)
            row.setValue("route_type", route_type)
            row.setValue("rt_typ_txt", route_type_text)
            row.setValue("route_url", route_url)
            row.setValue("rt_color", route_color)
            row.setValue("rt_col_fmt", route_color_formatted)
            row.setValue("rt_txt_col", route_text_color)
            row.setValue("rt_txt_fmt", route_text_color_formatted)
            StopsCursor.insertRow(row)
        else:
            row = StopsCursor.newRow()
            row.shape = polyline
            row.setValue("RouteShapeName", ShapeRoute)
            row.setValue("shape_id", shape)
            row.setValue("route_id", route)
            row.setValue("agency_id", agency_id)
            row.setValue("route_short_name", route_short_name)
            row.setValue("route_long_name", route_long_name)
            row.setValue("route_desc", route_desc)
            row.setValue("route_type", route_type)
            row.setValue("route_type_text", route_type_text)
            row.setValue("route_url", route_url)
            row.setValue("route_color", route_color)
            row.setValue("route_color_formatted", route_color_formatted)
            row.setValue("route_text_color", route_text_color)
            row.setValue("route_text_color_formatted",
                         route_text_color_formatted)
            StopsCursor.insertRow(row)
    else:
        # For everything 10.1 and forward
        StopsCursor.insertRow(
            (polyline, ShapeRoute, shape, route, agency_id, route_short_name,
             route_long_name, route_desc, route_type, route_type_text,
             route_url, route_color, route_color_formatted, route_text_color,
             route_text_color_formatted))
Example #30
0
def main():
    graph_connection = arcpy.GetParameterAsText(0)
    output_workspace = arcpy.GetParameterAsText(1)
    desc = arcpy.Describe(output_workspace)
    gc_tuple = literal_eval(graph_connection)

    g = Graph(gc_tuple[0], auth=(gc_tuple[1], gc_tuple[2]))
    arcpy.AddMessage("Successfully connected to Graph!")

    closeness = "CALL algo.closeness('Airport', 'CONNECTION', {write:true, writeProperty:'closeness'})"
    betweenness = "CALL algo.betweenness('Airport','CONNECTION', {direction:'out',write:true, writeProperty:'betweenness'})"
    in_degree = 'CALL algo.degree("Airport", "CONNECTION", {direction: "incoming", writeProperty: "in-degree"})'
    out_degree = 'CALL algo.degree("Airport", "CONNECTION", {direction: "outgoing", writeProperty: "out-degree"})'
    pagerank = "CALL algo.pageRank('Airport', 'CONNECTION',{iterations:20, dampingFactor:0.85, write: true,writeProperty:'pagerank'})"
    base_cypher = "START source=node(*) MATCH (source)-[r]->(target) RETURN source,r.airline,target;"

    # Checks to see if the output workspace is a file geodatabase.  If not, returns error and forces
    # user to use a file geodatabase.
    if desc.dataType != 'Workspace':
        arcpy.AddError(
            'Please select a file geodatabase to output your files.')
        exit()

    arcpy.AddMessage("Calculating closesness centrality.")
    g.run(closeness)
    arcpy.AddMessage("Calculating betweenness centrality.")
    g.run(betweenness)
    arcpy.AddMessage("Calculating in-degree centrality.")
    g.run(in_degree)
    arcpy.AddMessage("Calculating out-degree centrality.")
    g.run(out_degree)
    arcpy.AddMessage("Calculating pagerank centrality.")
    g.run(pagerank)

    results = g.run(base_cypher).data()

    arcpy.AddMessage('Starting ingest of data')

    # Sets the spatial reference for the output feature class to WGS 1984.
    sr = arcpy.SpatialReference(4326)

    # Creates the necessary feature classes
    arcpy.AddMessage("Creating feature classes")
    arcpy.CreateFeatureclass_management(output_workspace, 'Nodes_Centrality',
                                        "POINT", None, "DISABLED", "DISABLED",
                                        sr)
    arcpy.CreateFeatureclass_management(output_workspace, 'Edges', "POLYLINE",
                                        None, "DISABLED", "DISABLED", sr)
    node_fc = os.path.join(output_workspace, "Nodes_Centrality")
    edge_fc = os.path.join(output_workspace, "Edges")
    # Adds the necessary fields to the feature class
    arcpy.management.AddFields(
        node_fc,
        "name TEXT # 255 # #;location TEXT # 255 # #;state TEXT # 255 # #;country TEXT # 255 # #;start_date DATE;code TEXT # 10 # #; betweenness DOUBLE; closeness DOUBLE; in_degree DOUBLE; out_degree DOUBLE; pagerank DOUBLE"
    )
    arcpy.management.AddFields(
        edge_fc,
        "source_node TEXT 'Source Node' 255 # #;target_node TEXT 'Target Node' 255 # #;airline TEXT # 150 # #"
    )

    # Fields to be edited when creating the Insert Cursor objects for the node and edge tables
    node_fields = [
        "SHAPE@XY", "name", "location", "state", "country", "start_date",
        "code", "betweenness", "closeness", "in_degree", "out_degree",
        "pagerank"
    ]
    edge_fields = ["SHAPE@", "source_node", "target_node", "airline"]

    # Creates an instance of the Insert Cursor to add rows to the Nodes Table
    node_cursor = arcpy.da.InsertCursor(node_fc, node_fields)

    # Creates an instance of the Insert Cursor to add rows to the Edges Table
    edge_cursor = arcpy.da.InsertCursor(edge_fc, edge_fields)

    # Dictionary to store nodes to allow for a look up of coordinates for edge creation
    node_dict = {}

    # List object to store the base node/edge rows.  This is used in case a user wants to calculate
    # any of the centrality scores so the feature class can be edited in one swoop.
    node_rows = []
    edge_rows = []

    completed_nodes = []

    edges = []

    arcpy.AddMessage("Querying nodes and edges.")

    count = 0
    for r in results:
        count += 1
        source = dict(r['source'])
        target = dict(r['target'])
        airline = r["r.airline"]

        if count <= 10:
            arcpy.AddMessage(airline)

        edges.append((source['code'], target['code']))
        node_dict[target['code']] = (target['lon'], target['lat'])
        node_dict[source['code']] = (source['lon'], source['lat'])

        if source["code"] not in completed_nodes:
            source_row = [(float(source['lon']), float(source['lat'])),
                          source["name"], source["location"], source["state"],
                          source["country"], source["start_date"],
                          source["code"], source["betweenness"],
                          source["closeness"], source["in-degree"],
                          source["out-degree"], source["pagerank"]]

            completed_nodes.append(source['code'])
            node_rows.append(source_row)

        if target['code'] not in completed_nodes:
            target_row = [(float(target['lon']), float(target['lat'])),
                          target["name"], target["location"], target["state"],
                          target["country"], target["start_date"],
                          target["code"], target["betweenness"],
                          target["closeness"], target["in-degree"],
                          target["out-degree"], target["pagerank"]]

            completed_nodes.append(target['code'])

            node_rows.append(target_row)

        # Construct geometry of the edge
        source_point = arcpy.Point(float(node_dict[source['code']][0]),
                                   float(node_dict[source['code']][1]))
        target_point = arcpy.Point(float(node_dict[target['code']][0]),
                                   float(node_dict[target['code']][1]))
        line = arcpy.Polyline(arcpy.Array([source_point, target_point]))

        row = [line, source['name'], target['name'], airline]

        edge_rows.append(row)

    arcpy.AddMessage("Adding rows to Node Feature Class")

    arcpy.AddMessage("Adding nodes.")
    for row in node_rows:
        try:
            node_cursor.insertRow(row)
        except Exception as e:
            arcpy.AddMessage(row)
            arcpy.AddError(e)
            exit()

    # Delete the node cursor since it is no longer needed.
    del node_cursor

    arcpy.AddMessage("Adding edges.")

    # Adds items to the Edges feature class
    arcpy.AddMessage("Adding rows to Edge Feature Class")
    for row in edge_rows:
        try:
            edge_cursor.insertRow(row)
        except Exception as e:
            arcpy.AddError("Error on adding edges, exiting.")
            arcpy.AddError(e)
            exit()

    # Delete the edge cursor since it is no longer needed.
    del edge_cursor