1

In this script, I want to extract several polylines from one polyline.

The data comes from this link : Lignes aériennes RTE au 22 décembre 2017 INF 45 kV

In the flow, I merge the extracted polylines in order to form only one using the arcpy function union :

import arcpy

count = 2  # initialisation de la distance
incertitude = 0.5  # incertitude autour de la distance
distance = count
polyline = r"C:\lignes-aeriennes.shp"
request = "code_ligne = 'SSVI5L12URCUR'"
feature = arcpy.MakeFeatureLayer_management(polyline, "feature_class")
polyline_selection = 
arcpy.SelectLayerByAttribute_management("feature_class", "NEW_SELECTION", request)
mem_line = arcpy.CreateFeatureclass_management(r"C:\mem_line", "mem_line", "POLYLINE", "", "DISABLED", "DISABLED", polyline_selection)
print "mem_line --> " + str(mem_line)
arcpy.AddField_management(mem_line, "LineOID", "LONG")  # ajout du champ LineOID de type LONG
arcpy.AddField_management(mem_line, "Value", "FLOAT")  # ajout du champ Value de type FLOAT
result = arcpy.GetCount_management(polyline_selection)
print "result --> " + str(result)
features = int(result.getOutput(0))
print "features --> " + str(features)
search_fields = ["SHAPE@", "OID@"]  # polyline_selection
insert_fields = ["SHAPE@", "LineOID", "Value", "SHAPE@LENGTH"]  # mem_line
with arcpy.da.SearchCursor(polyline_selection, (search_fields)) as search:  # polyline_selection
    with arcpy.da.InsertCursor(mem_line, (insert_fields)) as insert:  # mem_line
        for row in search:  # pour chaque ligne
            line_geom = row[0]  # geometrie
            print "line_geom --> " + str(line_geom)
            length = float(line_geom.getLength('PRESERVE_SHAPE', 'METERS'))  # longueur
            print "length --> " + str(length)
            oid = str(row[1])  # ID
            print "oid --> " + str(oid)
            start = arcpy.PointGeometry(line_geom.firstPoint)  # debut
            print "start --> " + str(start)
            end = arcpy.PointGeometry(line_geom.lastPoint)  # fin
            print "end --> " + str(end)
            segment = line_geom.segmentAlongLine(count - incertitude, count + incertitude, False)  # extraction
            print "TYPE segment --> " + str(type(segment))
            segment_length = segment.getLength('PRESERVE_SHAPE', 'METERS')  # longueur
            print "new length --> " + str(segment_length)
            count += distance  # point suivant
            while count <= length:  # tant qu il reste de la longueur
                ligne = line_geom.segmentAlongLine(count - incertitude, count + incertitude, False)  # extraction
                ligne_length = ligne.getLength('PRESERVE_SHAPE', 'METERS')  # longueur
                print "ligne_length --> " + str(ligne_length)
                print "TYPE segment --> " + str(type(ligne))
                segment.union(ligne)  # ValueError possible !!!
                count += distance  # point suivant
            segment_length = segment.getLength('PRESERVE_SHAPE', 'METERS')  # longueur totale
            print "new length --> " + str(segment_length)
            insert.insertRow((segment, oid, count, segment_length))  # insertion
del polyline
del request
del feature
del polyline_selection
del mem_line

However, when the merge happens, a ValueError appears :

Traceback (most recent call last):

  File "<ipython-input-1-7582fbec95bb>", line 1, in <module>
    runfile('C:')

  File "C:\ProgramData\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "C:/exemple.py", line 44, in <module>
    segment.union(ligne)

  File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\arcobjects\arcobjects.py", line 855, in union
    return convertArcObjectToPythonObject(self._arc_object.Union(*gp_fixargs((other,))))

ValueError: <geoprocessing describe geometry object object at 0x09D657C0>

I don't understand this ValueError. This Polyline have the same type and are not corrupted, I can display them correctly in ArcMap.

2

I had the same problem when using a polyline created with the segmentAlongLine method.

I solved it like this:

fixed_segment = arcpy.Polyline(segment.getPart(0), segment.spatialReference)

Assuming that there is only one part in the polyline.

| improve this answer | |
1

I tried the following code and it did not yield a ValueError.

>>> import arcpy
>>> p1 = arcpy.Polyline(arcpy.Array([arcpy.Point(0,0), arcpy.Point(5,5)]))
>>> p2 = arcpy.Polyline(arcpy.Array([arcpy.Point(2,2), arcpy.Point(4,4)]))
>>> p1.union(p2)
<Polyline object at 0x302bb50[0x302bea0]>
>>> p2 = arcpy.Polyline(arcpy.Array([arcpy.Point(20,20), arcpy.Point(40,40)]))
>>> p1.union(p2)
<Polyline object at 0x302b9f0[0x3885cc0]>
>>> p1.length
7.0710678118654755
>>> p2.length
28.284271247461902

So I tried it when the lines intersect and not at all. So I was wondering if your data (which you don't actually explain anything about) is incompatible or corrupt?

For example how have you constructed the geometries? May be you created corrupted geometries or are you retrieving them from a data source? Are they the same coordinate system?

Based upon my simple example, my money is on the source of your data which you have not adequately described for others to help.

| improve this answer | |
  • 1
    You're certainly right... I retrieve the data by the function arcpy.da.SearchCursor. Even if I succes to display them in ArcMap, the use of the function arcpy.Describe(Polyline).spatialReference send the same ValueError. There is a problem with data :'( – Rudymil1 Jun 12 '18 at 14:09
  • @Rudymil1 so what is your data source? – Hornbydd Jun 12 '18 at 16:11
  • In fact I extract polylines from a GDB (arcpy.da.SearchCursor). Then, for each polyline, I extract some pieces (polyligne.segmentAlongLine) that I merge then (polyligne.union (polyline)). And that's where it goes wrong. – Rudymil1 Jun 13 '18 at 7:06
  • And to answer your question, I retrieve the data on this link : Lignes aériennes RTE au 22 décembre 2017 INF 45 kV – Rudymil1 Jun 13 '18 at 7:27
  • 1
    @Rudymil1 can you add to your original question the code that extracts the geometries , creates a segment then unions them. What are you union them with? Looking at your sample data none of the lines overlap. So if you are attempting to union the lines with themselves I would expect Null geometries returned but that would not explain the ValueError message. – Hornbydd Jun 13 '18 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.