Example #1
0
def calc_linha_ret(li_pla1, li_pla2, distancia, proj_geo, proj_plana):
    array = Array()
    ponto1 = li_pla1.positionAlongLine(distancia).projectAs(proj_geo)
    ponto2 = li_pla2.positionAlongLine(distancia).projectAs(proj_geo)
    pt_label1 = ponto1.labelPoint
    pt_label2 = ponto2.labelPoint
    array.add(pt_label1)
    array.add(pt_label2)
    linha_ret = Polyline(array, proj_geo)
    del array
    return linha_ret
Example #2
0
def rotacionar_poligono(polygon, anchor_point, ang, proj_geo):
    array = Array()
    anchor_x = anchor_point.X
    anchor_y = anchor_point.Y
    for part in polygon:
        for point in part:
            x = point.X - anchor_x
            y = point.Y - anchor_y
            resultx = (x * math.cos(ang)) - (y * math.sin(ang)) + anchor_x
            resulty = (x * math.sin(ang)) + (y * math.cos(ang)) + anchor_y
            array.add(Point(resultx, resulty))
    result_polygon = Polygon(array, proj_geo)
    del array
    return result_polygon
Example #3
0
def _trim_shoreline(islands):
    """
    Trim the shoreline of micro-islands. This makes 
    permanent changes to the Shapefile.
    """
    for island in islands:
        path = path_to_shoreline(island)
        pair = max(TableToNumPyArray(path, ["OID@", "SHAPE@AREA"]),
                   key=lambda p: p[1])
        with UpdateCursor(path, ["OID@", "SHAPE@"]) as cursor:
            for row in cursor:
                if row[0] != pair[0]:
                    cursor.deleteRow()
                else:
                    row_new = Array()
                    for part in row[1]:
                        part_new = Array()
                        for point in part:
                            if point is None:
                                break
                            part_new.add(point)
                        row_new.add(part_new)
                    row[1] = Polygon(row_new)
                    cursor.updateRow(row)
Example #4
0
def ret_envolvente(polygon, proj_geo):
    array = Array()
    array.add(polygon.extent.lowerLeft)
    array.add(polygon.extent.lowerRight)
    array.add(polygon.extent.upperRight)
    array.add(polygon.extent.upperLeft)
    array.add(polygon.extent.lowerLeft)
    result_polygon = Polygon(array, proj_geo)
    del array
    return result_polygon
Example #5
0
def bases_larguras(polygon, proj_geo):
    array1 = Array()
    array2 = Array()
    array3 = Array()
    array4 = Array()
    point1 = None
    point2 = None
    point3 = None
    point4 = None
    for part in polygon:
        point1 = part[0]
        point2 = part[1]
        point3 = part[2]
        point4 = part[3]
    array1.add(point1); array1.add(point2)
    array2.add(point2); array2.add(point3)
    array3.add(point4); array3.add(point3)
    array4.add(point1); array4.add(point4)
    li1 = Polyline(array1, proj_geo)
    li2 = Polyline(array2, proj_geo)
    li3 = Polyline(array3, proj_geo)
    li4 = Polyline(array4, proj_geo)
    if li1.length > li2.length:
        lista_base = [li1, li3]
        lista_largura = [li2, li4]
    else:
        lista_base = [li2, li4]
        lista_largura = [li1, li3]
    del array1
    del array2
    del array3
    del array4
    return lista_base, lista_largura
def extendAndIntersectRoadFeatures(
    quarterOrHalf
):  # Place the operations that extend each road line segment by a certain distance here.
    # Should extend all the features that exist in the post-erase dataset. Might be more difficult
    # to calculate the angle of these lines accurately, but it should be easier to figure out
    # than trying to get the lines to split correctly with the buggy SplitLineAtPoint tool.

    if quarterOrHalf.lower() == "quarter":
        extensionLinesTextName = "createdExtensionLines_Q"
        createdExtensionLines = createdExtensionLines_Q
        # 9000 ft increase for _Q version.
        # Must be larger than the county bufferDistance (20000)
        extensionDistance = 31176
        extensionLinesTextName = "createdExtensionLines_Q"
        countyRoadNameRosette = countyRoadNameRosette_Q
        rosetteTextName = "countyRoadNameRosette_Q"
        tempRoadNameRosette = tempRoadNameRosette_Q
        tempRosetteTextName = "tempRoadNameRosette_Q"
        tempRoadNameRosetteSP = tempRoadNameRosetteSinglePoint_Q
        tempRosetteSPTextName = "tempRoadNameRosetteSinglePoint_Q"
        countyBorderFeature = countyBorderFeature_Q
    elif quarterOrHalf.lower() == "half":
        extensionLinesTextName = "createdExtensionLines_H"
        createdExtensionLines = createdExtensionLines_H
        # Must be larger than the county bufferDistance (11000)
        extensionDistance = 22176
        extensionLinesTextName = "createdExtensionLines_H"
        countyRoadNameRosette = countyRoadNameRosette_H
        rosetteTextName = "countyRoadNameRosette_H"
        tempRoadNameRosette = tempRoadNameRosette_H
        tempRosetteTextName = "tempRoadNameRosette_H"
        tempRoadNameRosetteSP = tempRoadNameRosetteSinglePoint_H
        tempRosetteSPTextName = "tempRoadNameRosetteSinglePoint_H"
        countyBorderFeature = countyBorderFeature_H
    else:
        print "quarterOrHalf variable not correctly defined."
        raise (Exception("quarterOrHalf value error."))

    print "Starting to extend and intersect road features."

    if Exists(createdExtensionLines):
        Delete_management(createdExtensionLines)
    else:
        pass

    CreateFeatureclass_management(inMemGDB, extensionLinesTextName, "POLYLINE",
                                  "", "", "", spatialReferenceProjection)

    # Add a column for roadname called roadNameForSplit.
    AddField_management(createdExtensionLines, "roadNameForSplit", "TEXT", "",
                        "", "55")

    # Add a column which stores the angle to display a label called called LabelAngle.
    AddField_management(createdExtensionLines, "LabelAngle", "DOUBLE", "", "",
                        "")  # Change to double.

    # Add a column which stores the County Number.
    AddField_management(createdExtensionLines, "County_Number", "DOUBLE", "",
                        "", "")

    roadLinesToInsertList = list()

    roadLinesList = getRoadLinesList()

    for roadLinesItem in roadLinesList:

        roadNameToUse = roadLinesItem[2]
        countyNumber = roadLinesItem[3]

        linePointsArray = ArcgisArray()

        firstPointTuple = (roadLinesItem[1].firstPoint.X,
                           roadLinesItem[1].firstPoint.Y)
        lastPointTuple = (roadLinesItem[1].lastPoint.X,
                          roadLinesItem[1].lastPoint.Y)

        # Make this a two-step process.
        # Might be as simple as
        # adding _1 to the end of the first set of variables,
        # adding _2 to the end of the second set of variables,
        # then making the extensions in both directions
        # and creating a new line that has the endpoints
        # from both sides as it's first and last point.
        # if necessary, could add the other points in between
        # but probably not necessary just for generating
        # an intersection point.

        yValue_1 = -(lastPointTuple[1] - firstPointTuple[1]
                     )  # made y value negative
        xValue_1 = lastPointTuple[0] - firstPointTuple[0]

        lineDirectionAngle_1 = math.degrees(math.atan2(
            xValue_1, yValue_1))  # reversed x and y

        lineDirectionAngle_1 = -(((lineDirectionAngle_1 + 180) % 360) - 180
                                 )  # correction for certain quadrants
        #print "lineDirectionAngle: " + str(lineDirectionAngle_1)

        origin_x_1 = firstPointTuple[0]
        origin_y_1 = firstPointTuple[1]

        yValue_2 = -(firstPointTuple[1] - lastPointTuple[1]
                     )  # made y value negative
        xValue_2 = firstPointTuple[0] - lastPointTuple[0]

        lineDirectionAngle_2 = math.degrees(math.atan2(
            xValue_2, yValue_2))  # reversed x and y

        lineDirectionAngle_2 = -(((lineDirectionAngle_2 + 180) % 360) - 180
                                 )  # correction for certain quadrants
        #print "lineDirectionAngle: " + str(lineDirectionAngle_2)

        origin_x_2 = lastPointTuple[0]
        origin_y_2 = lastPointTuple[1]

        (disp_x_1, disp_y_1) = (extensionDistance *
                                math.sin(math.radians(lineDirectionAngle_1)),
                                extensionDistance *
                                math.cos(math.radians(lineDirectionAngle_1)))

        (end_x_1, end_y_1) = (origin_x_1 + disp_x_1, origin_y_1 + disp_y_1)

        (disp_x_2, disp_y_2) = (extensionDistance *
                                math.sin(math.radians(lineDirectionAngle_2)),
                                extensionDistance *
                                math.cos(math.radians(lineDirectionAngle_2)))

        (end_x_2, end_y_2) = (origin_x_2 + disp_x_2, origin_y_2 + disp_y_2)

        startPoint = ArcgisPoint()
        endPoint = ArcgisPoint()

        startPoint.ID = 0
        startPoint.X = end_x_1
        startPoint.Y = end_y_1

        endPoint.ID = 1
        endPoint.X = end_x_2
        endPoint.Y = end_y_2

        linePointsArray.add(startPoint)
        linePointsArray.add(endPoint)

        newLineFeature = ArcgisPolyLine(linePointsArray)

        # Need to create an extension for both ends of the line and add them
        # to the array.

        #newLineFeature = createdExtensionLinesCursor.newRow()

        #newLineFeature.SHAPE = linePointsArray

        lineDirectionOutput = "0"

        if lineDirectionAngle_1 > 0:
            lineDirectionOutput = lineDirectionAngle_1
        elif lineDirectionAngle_2 > 0:
            lineDirectionOutput = lineDirectionAngle_2
        else:
            pass

        roadLinesToInsertList.append(
            [newLineFeature, roadNameToUse, lineDirectionOutput, countyNumber])

        #createdExtensionLinesCursor.insertRow([newLineFeature, roadNameToUse, lineDirectionOutput])

        if "newLineFeature" in locals():
            del newLineFeature
        else:
            pass

    # Consider building this as a separate list and then just looping
    # through the list to put it into the cursor instead
    # of doing logic and inserting into the cursor at the same place.

    #start editing session
    #newEditingSession = daEditor(sqlGdbLocation)
    #newEditingSession.startEditing()
    #newEditingSession.startOperation()

    createdExtensionLinesCursor = daInsertCursor(
        createdExtensionLines,
        ["SHAPE@", "roadNameForSplit", "LabelAngle", "County_Number"])

    for roadLinesToInsertItem in roadLinesToInsertList:
        createdExtensionLinesCursor.insertRow(roadLinesToInsertItem)

    # End editing session
    #newEditingSession.stopOperation()
    #newEditingSession.stopEditing(True)

    if "createdExtensionLinesCursor" in locals():
        del createdExtensionLinesCursor
    else:
        pass

    # Remove the previous countyRoadNameRosette so that it can be recreated.
    if Exists(rosetteTextName):
        Delete_management(rosetteTextName)
    else:
        pass

    CreateFeatureclass_management(sqlGdbLocation, rosetteTextName, "POINT", "",
                                  "", "", spatialReferenceProjection)

    AddField_management(countyRoadNameRosette, "roadNameForSplit", "TEXT", "",
                        "", "55")

    AddField_management(countyRoadNameRosette, "LabelAngle", "DOUBLE", "", "",
                        "")  # Change to double.

    AddField_management(countyRoadNameRosette, "County_Number", "DOUBLE", "",
                        "", "")

    AddField_management(countyRoadNameRosette, "COUNTY_NAME", "TEXT", "", "",
                        "55")

    # Now then, need to check for the existence
    # of and delete the point intersection layer
    # if it exists.

    # Then, recreate it and the proper fields.

    inMemoryCountyBorderExtension = "aCountyBorderExtensionBuffer"
    inMemoryExtensionLines = "aLoadedExtensionLines"

    try:
        Delete_management(inMemoryCountyBorderExtension)
    except:
        pass

    try:
        Delete_management(inMemoryExtensionLines)
    except:
        pass

    # Temporary layer, use CopyFeatures_management to persist to disk.
    MakeFeatureLayer_management(
        countyBorderFeature,
        inMemoryCountyBorderExtension)  # County Border extension feature

    # Temporary layer, use CopyFeatures_management to persist to disk.
    MakeFeatureLayer_management(
        createdExtensionLines,
        inMemoryExtensionLines)  # Line extension feature

    borderFeatureList = getBorderFeatureList(quarterOrHalf)

    borderFeatureList = sorted(borderFeatureList,
                               key=lambda feature: feature[3])

    for borderFeature in borderFeatureList:
        borderFeatureName = borderFeature[2]
        borderFeatureNumber = borderFeature[3]
        print "borderFeatureName: " + str(
            borderFeatureName) + " & borderFeatureNumber: " + str(
                int(borderFeatureNumber))

        countyBorderWhereClause = ' "COUNTY_NUMBER" = ' + str(
            int(borderFeatureNumber)) + ' '

        SelectLayerByAttribute_management(inMemoryCountyBorderExtension,
                                          "NEW_SELECTION",
                                          countyBorderWhereClause)

        countyBorderSelectionCount = GetCount_management(
            inMemoryCountyBorderExtension)

        print "County Borders Selected: " + str(countyBorderSelectionCount)

        # Had to single-quote the borderFeatureNumber because it is stored as a string in the table.
        # Unsingle quoted because it was changed to a float.
        extensionLinesWhereClause = ' "COUNTY_NUMBER" = ' + str(
            int(borderFeatureNumber)) + ' '

        SelectLayerByAttribute_management(inMemoryExtensionLines,
                                          "NEW_SELECTION",
                                          extensionLinesWhereClause)

        extensionLineSelectionCount = GetCount_management(
            inMemoryExtensionLines)

        print "Extension Lines Selected: " + str(extensionLineSelectionCount)

        if Exists(tempRosetteTextName):
            Delete_management(tempRosetteTextName)
        else:
            pass

        if Exists(tempRosetteSPTextName):
            Delete_management(tempRosetteSPTextName)
        else:
            pass

        Intersect_analysis(
            [inMemoryCountyBorderExtension, inMemoryExtensionLines],
            tempRoadNameRosette, "ALL", "", "POINT")

        # Intersect to an output temp layer.

        # Next, need to loop through all of the counties.

        # Get the county number and use it to select
        # a county extension buffer in the county
        # extension buffers layer.

        # Then, use the county number to select
        # all of the lines for that county
        # in the extension lines layer.

        # Then, export those to a temp layer in the fgdb.

        # Change multipoint to singlepoint.

        # Was working until I moved from gisprod to sdedev for the data source.
        # not sure why. Check to make sure projections match.
        # ^ Fixed.

        try:

            # Run the tool to create a new fc with only singlepart features
            MultipartToSinglepart_management(tempRoadNameRosette,
                                             tempRoadNameRosetteSP)

            # Check if there is a different number of features in the output
            #   than there was in the input
            inCount = int(
                GetCount_management(tempRoadNameRosette).getOutput(0))
            outCount = int(
                GetCount_management(tempRoadNameRosetteSP).getOutput(0))

            if inCount != outCount:
                print "Found " + str(outCount -
                                     inCount) + " multipart features."
                #print "inCount, including multipart = " + str(inCount)
                #print "outCount, singlepart only = " + str(outCount)

            else:
                print "No multipart features were found"

        except ExecuteError:
            print GetMessages()
        except Exception as e:
            print e

        print "Appending the temp point layer to the county point intersection layer."

        Append_management([tempRoadNameRosetteSP], countyRoadNameRosette,
                          "NO_TEST")

        # K, worked correctly. Just need to change LabelAngle to a float and it might be what
        # I want.

        print "Done adding points to the countyRoadNameRosette feature class."
Example #7
0
def criar_poligono_app(linha_app, linha_app_frente):
    array = Array()
    l1_firstX = linha_app.firstPoint.X
    l1_firstY = linha_app.firstPoint.Y
    l1_lastX = linha_app.lastPoint.X
    l1_lastY = linha_app.lastPoint.Y
    l2_firstX = linha_app_frente.firstPoint.X
    l2_firstY = linha_app_frente.firstPoint.Y
    l2_lastX = linha_app_frente.lastPoint.X
    l2_lastY = linha_app_frente.lastPoint.Y

    array.add(linha_app.firstPoint)
    array.add(linha_app.lastPoint)
    if bool_interseccao_entre_linhas(((l1_lastX,l1_lastY),(l2_lastX,l2_lastY)),((l1_firstX,l1_firstY),(l2_firstX,l2_firstY))):
        array.add(linha_app_frente.firstPoint)
        array.add(linha_app_frente.lastPoint)
    else:
        array.add(linha_app_frente.lastPoint)
        array.add(linha_app_frente.firstPoint)
    array.add(linha_app.firstPoint)
    polygon = Polygon(array, projecao_geo)
    array.removeAll()
    del array
    polygon = polygon.buffer(0.000000001)
    return polygon
pt = Point()

### Préalables
new_chp(shp_in, chp_x, "DOUBLE", 18, 11)
new_chp(shp_in, chp_y, "DOUBLE", 18, 11)


### Calcul des centroids
calc_chp(shp_in, chp_x, expr_centrX, "PYTHON")
calc_chp(shp_in, chp_y, expr_centrY, "PYTHON")

rows = curs_rec(shp_in)

i = 0
for objet in rows:
    pt.ID = i
    pt.X = objet.getValue(chp_x)
    pt.Y = objet.getValue(chp_y)

    feat = add.newRow()
    feat.shape = pt
    add.insertRow(feat)
    ptArray.add(pt)

    i = i +1



del add, curs_ins, curs_rec, chp_x, chp_y, \
    expr_centrX, expr_centrY, feat, i, pt, rows, ptArray, shp_in, shp_out