Beispiel #1
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
Beispiel #2
0
def calc_poligono_ponta(linha_prox, linha_anterior):
    linha_prox_buff = linha_prox.projectAs(projecao_plana).buffer(1).projectAs(projecao_geo)
    poligono_ma_separado = poligono_ma.difference(linha_prox_buff)
    if poligono_ma_separado.isMultipart:
        for n in range(poligono_ma_separado.partCount):
            parte = poligono_ma_separado.getPart(n)
            poly_ma_parte = Polygon(parte, projecao_geo)
            if  poly_ma_parte.disjoint(linha_anterior):
                return poly_ma_parte
def fc_union(in_fc, poly_type="polygon"):
    """Union features in a featureclass.

    The output shape is built from its individual parts.
    Shared boundaries will be dissolved.

    Parameters
    ----------
    in_fc : featureclass
    poly_type : text
        Either `polygon` or `polyline`
    fc = "C:/Git_Dan/npgeom/Project_npg/tests.gdb/sq"
    """
    arr = []
    SR = get_SR(in_fc, verbose=False)
    with SearchCursor(in_fc, ['SHAPE@']) as cursor:
        for row in cursor:
            poly = row[0]
            for cnt in range(poly.partCount):
                part = poly.getPart(cnt)
                arr.append(part)
    a = Array(arr)
    if poly_type == "polygon":
        return Polygon(a, SR)
    elif poly_type == "polyline":
        return Polyline(a, SR)
    else:
        print("Not polygon or polyline")
        return None
def _array_to_poly_(arr, SR=None, as_type="Polygon"):
    """Convert array-like objects to arcpy geometry.

    This can include an `ndarray`, an `object array` or a `list of lists`
    which represent polygon or polyline geometry.

    Parameters
    ----------
    arr : list-like
        A list of arrays representing the poly parts, or an object array.
    SR : spatial reference
        Leave as None if not known.  Enclose in quotes. eg. "4326"
    as_type : text
        Polygon or Polyline.

    Notes
    -----
    Polygon geometries require a duplicate first and last point.
    Outer rings are ordered clockwise with inner rings (holes)
    counterclockwise.
    No check are made to the integrety of the geometry in this function.
    """
    subs = np.asarray(arr, dtype='O')
    aa = []
    for sub in subs:
        aa.append([Point(*pairs) for pairs in sub])
    if as_type.upper() == 'POLYGON':
        poly = Polygon(Array(aa), SR)
    elif as_type.upper() == 'POLYLINE':
        poly = Polyline(Array(aa), SR)
    return poly
Beispiel #5
0
def _load_polygons(
        polygon_file: os.PathLike) -> Generator[Polygon, None, None]:
    """
    Extract polygons from the given filename

    Parameters
    ----------
    polygon_file
        Path to a CSV file with format:
            12345, 3.141592, 1.337
        Where the columns are, in order:
            - Unique ID of the curve being described
            - X value of the point
            - Y value of the point

    Notes
    -----
    I assume the points that make up an `Array` or `Polygon` are all adjacent to each other.
    If this were not the case, I would probably use a `dict[int, Array]` to accumulate `Point`s
    and then do a post-processing pass to turn the accumulated results into `Polygon`s.
    """
    filepth = Path(polygon_file)
    points = []
    for line in fileinput.input(filepth):
        pid, x, y = line.split(",")
        pid = int(pid)
        x = float(x)
        y = float(y)
        pt = Point(ID=pid, X=x, Y=y)
        if points and points[-1].ID != pt.ID:
            # if we have accumulated points and the ID changed, we're starting a new Polygon, so yield the old one first
            arr = PointArray(points)
            polygon = Polygon(arr)
            points = [pt]
            yield polygon
        else:
            # we're still building the next Polygon, accumulate this point
            points.append(pt)

    if points:
        arr = PointArray(points)
        polygon = Polygon(arr)
        yield polygon
    def arr2poly(a, SR):
        """Construct the poly feature from lists or arrays.

        The inputs may be nested and mixed in content.
        """
        aa = []
        for pairs in a:
            sub = pairs[0]
            oid = pairs[1]
            aa.append([Point(*pairs) for pairs in sub])
        if p_type.upper() == 'POLYGON':
            poly = Polygon(Array(aa), SR)
        elif p_type.upper() == 'POLYLINE':
            poly = Polyline(Array(aa), SR)
        return (poly, oid)
def raster_extent_polygon(in_raster):
    from arcpy import Array, Point, Polygon, Describe
    desc = Describe(in_raster)
    XMin = desc.extent.XMin
    XMax = desc.extent.XMax
    YMin = desc.extent.YMin
    YMax = desc.extent.YMax
    # Create a polygon geometry
    array = Array([
        Point(XMin, YMin),
        Point(XMin, YMax),
        Point(XMax, YMax),
        Point(XMax, YMin)
    ])
    return Polygon(array)
 def _arr_poly_(arr, SR, as_type):
     """Slice the array where nan values appear, splitting them off."""
     subs = []
     s = np.isnan(arr[:, 0])
     if np.any(s):
         w = np.where(s)[0]
         ss = np.split(arr, w)
         subs = [ss[0]]
         subs.extend(i[1:] for i in ss[1:])
     else:
         subs.append(arr)
     aa = []
     for sub in subs:
         aa.append([Point(*pairs) for pairs in sub])
     if as_type.upper() == 'POLYGON':
         poly = Polygon(Array(aa), SR)
     elif as_type.upper() == 'POLYLINE':
         poly = Polyline(Array(aa), SR)
     return poly
def view_poly(geo, id_num=1, view_as=2):
    """View a single poly feature as an SVG in the console.

    Parameters
    ----------
    geo : Geo array
        The Geo array part to view.
    id_num : integer
        The shape in the Geo array to view.
    view_as : integer
        Polygon = 2, Polygon = 1, Multipoint = 0

    Notes
    -----
    These provide information on the content of the svg representation.

    >>> p0.__getSVG__()
    >>> p0._repr_svg_()
    f = [" M {},{} " + "L {},{} "*(len(b) - 1) for b in g0.bits]
    ln = [f[i].format(*b.ravel()) for i, b in enumerate(g0.bits)]
    st = "".join(ln) + "z"
    """
    if id_num not in (geo.IDs):
        msg = "Id ... {} ... not found.\n Use geo.IDs to see their values"
        print(msg.format(id_num))
        return
    shp = geo.get_shapes(id_num)
    z = [Array([Point(*i) for i in b]) for b in shp.bits]
    if view_as == 2:
        return Polygon(Array(z))
    elif view_as == 1:
        return Polyline(Array(z))
    else:
        zz = []
        for i in z:
            zz.extend(i)
        return Multipoint(Array(zz))
Beispiel #10
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)
def generate_squares(in_polygon, in_raster):
    from arcpy import Describe, Array, Point, Polygon, da
    desc = Describe(in_raster)
    eXMin = desc.extent.XMin
    eYMin = desc.extent.YMin
    eXMax = desc.extent.XMax
    eYMax = desc.extent.YMax

    offset = 1
    sqLen = 1
    # Store extent values as list of coordinate
    blX = eXMin - offset
    blY = eYMin - offset
    bottom_left_square = Array([
        Point(blX - sqLen, blY - sqLen),
        Point(blX - sqLen, blY),
        Point(blX, blY),
        Point(blX, blY - sqLen)
    ])
    trX = eXMax + offset
    trY = eYMax + offset
    top_right_square = Array([
        Point(trX, trY),
        Point(trX, trY + sqLen),
        Point(trX + sqLen, trY + sqLen),
        Point(trX + sqLen, trY)
    ])
    # Get coordinate system
    # Open an InsertCursor and insert the new geometry
    cursor = da.InsertCursor(in_polygon, ['SHAPE@'])
    for sq in [bottom_left_square, top_right_square]:
        # Create a polygon geometry
        polygon = Polygon(sq)
        cursor.insertRow([polygon])
    # Delete cursor object
    del cursor