Changeset 1027


Ignore:
Timestamp:
08/28/12 17:22:32 (5 years ago)
Author:
jjr8
Message:

Working on enhancements to the CreatePointsAlongLines? tool for bbest.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • MGET/Branches/Jason/PythonPackage/src/GeoEco/SpatialAnalysis/Points.py

    r977 r1027  
    341341 
    342342    @classmethod 
    343     def CreatePointsAlongLines(cls, inputLines, outputFeatureClass, interval, unit, trackIDField=None, orderByField=None, startTimeField=None, endTimeField=None, pointsAtLineEnds=False, distributeRemainder=True, where=None, fieldsToCopy=None, overwriteExisting=False): 
     343    def CreatePointsAlongLines(cls, inputLines, outputFeatureClass, interval, unit, trackIDField=None, orderByField=None, startTimeField=None, endTimeField=None, pointsAtLineEnds=False, distributeRemainder=True, where=None, fieldsToCopy=None, distanceField=u'Distance', pointTimeField=u'PointTime', elapsedField=u'Elapsed', elapsedUnit=u'Hours', overwriteExisting=False): 
    344344        cls.__doc__.Obj.ValidateMethodInvocation() 
    345345        try: 
     
    347347            # Perform additional validation. 
    348348 
    349             isTemporal = unit in [u'days', u'hours', u'minutes', u'seconds'] 
    350             if isTemporal: 
     349            temporalInterval = unit in [u'days', u'hours', u'minutes', u'seconds'] 
     350            if temporalInterval: 
    351351                if startTimeField is None: 
    352352                    raise ValueError(_(u'A value must be provided for the Start Time Field parameter when the Interval Unit is %(unit)s.') % {u'unit': unit}) 
     
    376376            # interval to a datetime.timedelta instance. 
    377377 
    378             if isTemporal: 
     378            if temporalInterval: 
    379379                if unit == u'days': 
    380380                    interval = datetime.timedelta(interval) 
     
    436436                i += 1 
    437437 
    438             if isTemporal: 
     438            if temporalInterval: 
    439439                outputTable.AddField(u'PointTime', u'datetime') 
    440440 
     
    473473                            trackIsValid = True 
    474474 
    475                             if isTemporal: 
     475                            if temporalInterval: 
    476476                                trackLength = datetime.timedelta(0) 
    477477                            else: 
     
    481481                            continue 
    482482 
    483                         if isTemporal: 
     483                        if temporalInterval: 
    484484                            duration = selectCursor.GetValue(endTimeField) - selectCursor.GetValue(startTimeField) 
    485485                            if duration < datetime.timedelta(0): 
     
    488488                            trackLength += duration 
    489489                        else: 
    490                             trackLength += cls._GetLineLength(linesSR, unit, selectCursor, isTemporal) 
     490                            trackLength += cls._GetLineLength(linesSR, unit, selectCursor, temporalInterval) 
    491491                finally: 
    492492                    del selectCursor 
     
    501501 
    502502                for trackID, length in trackLengths.items(): 
    503                     if isTemporal: 
     503                    if temporalInterval: 
    504504                        intervalInSeconds = interval.microseconds * 0.000001 + interval.seconds + interval.days * 86400. 
    505505                        lengthInSeconds = length.microseconds * 0.000001 + length.seconds + length.days * 86400. 
     
    536536                            fieldValues[destFields[i]] = selectCursor.GetValue(sourceFields[i]) 
    537537 
    538                         if isTemporal: 
     538                        if temporalInterval: 
    539539                            startTime = selectCursor.GetValue(startTimeField) 
    540540                            endTime = selectCursor.GetValue(endTimeField) 
     
    552552                        if trackIDField is not None: 
    553553                            newTrackID = selectCursor.GetValue(trackIDField) 
    554                             if newTrackID != trackID and pointsAtLineEnds and trackIsValid and (isTemporal and distanceSincePoint > datetime.timedelta(0) or not isTemporal and distanceSincePoint > 0) and endX is not None: 
     554                            if newTrackID != trackID and pointsAtLineEnds and trackIsValid and (temporalInterval and distanceSincePoint > datetime.timedelta(0) or not temporalInterval and distanceSincePoint > 0) and endX is not None: 
    555555                                Logger.Debug('Track %r: Writing end point' % trackID) 
    556556                                cls._InsertPointForLine(endX, endY, endTime, insertCursor, destFields, fieldValues) 
     
    567567                            startOfTrack = True 
    568568                             
    569                             if isTemporal: 
     569                            if temporalInterval: 
    570570                                distanceSincePoint = datetime.timedelta(0) 
    571571                            else: 
     
    613613                        # calculate the duration of this line. 
    614614 
    615                         if isTemporal: 
     615                        if temporalInterval: 
    616616                            duration = endTime - startTime 
    617617                            Logger.Debug('Track %r: Duration of line = %r, distance to next point = %r.' % (trackID, duration, distanceToNextPoint)) 
     
    635635                            # distance along this line to that point. 
    636636 
    637                             rate = cls._GetLineLength(linesSR, unit, selectCursor, isTemporal) / (duration.microseconds * 0.000001 + duration.seconds + duration.days * 86400.) 
     637                            rate = cls._GetLineLength(linesSR, unit, selectCursor, temporalInterval) / (duration.microseconds * 0.000001 + duration.seconds + duration.days * 86400.) 
    638638                            spatialDistanceToNextPoint = rate * (distanceToNextPoint.microseconds * 0.000001 + distanceToNextPoint.seconds + distanceToNextPoint.days * 86400.) 
    639639 
     
    671671 
    672672                                while True: 
    673                                     distanceToEnd = cls._GetDistance(currentX, currentY, endX, endY, linesSR, unit, isTemporal) 
     673                                    distanceToEnd = cls._GetDistance(currentX, currentY, endX, endY, linesSR, unit, temporalInterval) 
    674674                                     
    675                                     if not isTemporal: 
     675                                    if not temporalInterval: 
    676676                                        if distanceToEnd < distanceToNextPoint: 
    677677                                            distanceSincePoint += distanceToEnd 
     
    679679                                            break 
    680680 
    681                                         pointX, pointY = cls._GetNextPointCoords(currentX, currentY, endX, endY, distanceToNextPoint, linesSR, unit, isTemporal) 
     681                                        pointX, pointY = cls._GetNextPointCoords(currentX, currentY, endX, endY, distanceToNextPoint, linesSR, unit, temporalInterval) 
    682682                                        cls._InsertPointForLine(pointX, pointY, None, insertCursor, destFields, fieldValues) 
    683683 
     
    698698                                            break 
    699699 
    700                                         pointX, pointY = cls._GetNextPointCoords(currentX, currentY, endX, endY, spatialDistanceToNextPoint, linesSR, unit, isTemporal) 
     700                                        pointX, pointY = cls._GetNextPointCoords(currentX, currentY, endX, endY, spatialDistanceToNextPoint, linesSR, unit, temporalInterval) 
    701701                                        cls._InsertPointForLine(pointX, pointY, endTime - datetime.timedelta(seconds=distanceToEnd / rate) + distanceToNextPoint, insertCursor, destFields, fieldValues) 
    702702 
     
    719719                    # the last track, insert the end point. 
    720720                     
    721                     if pointsAtLineEnds and trackIsValid and (isTemporal and distanceSincePoint > datetime.timedelta(0) or not isTemporal and distanceSincePoint > 0) and endX is not None: 
     721                    if pointsAtLineEnds and trackIsValid and (temporalInterval and distanceSincePoint > datetime.timedelta(0) or not temporalInterval and distanceSincePoint > 0) and endX is not None: 
    722722                        Logger.Debug('Track %r: Writing end point' % trackID) 
    723723                        cls._InsertPointForLine(endX, endY, endTime, insertCursor, destFields, fieldValues) 
     
    742742 
    743743    @classmethod 
    744     def _GetLineLength(cls, linesSR, unit, selectCursor, isTemporal): 
     744    def _GetLineLength(cls, linesSR, unit, selectCursor, temporalInterval): 
    745745        lengthInMeters = 0. 
    746746        geometry = selectCursor.GetGeometry() 
     
    758758            # the haversine formula. 
    759759 
    760             if not linesSR.IsProjected() and unit != 'decimal degrees' and not isTemporal: 
     760            if not linesSR.IsProjected() and unit != 'decimal degrees' and not temporalInterval: 
    761761                for i in range(1, part.GetPointCount()): 
    762762                    lengthInMeters += cls._HaversineDistance(part.GetX(i), part.GetY(i), part.GetX(i-1), part.GetY(i-1)) 
     
    779779 
    780780    @classmethod 
    781     def _GetDistance(cls, x1, y1, x2, y2, linesSR, unit, isTemporal): 
    782         if not linesSR.IsProjected() and unit != 'decimal degrees' and not isTemporal: 
     781    def _GetDistance(cls, x1, y1, x2, y2, linesSR, unit, temporalInterval): 
     782        if not linesSR.IsProjected() and unit != 'decimal degrees' and not temporalInterval: 
    783783            return cls._HaversineDistance(x1, y1, x2, y2) 
    784784        if not linesSR.IsProjected(): 
     
    787787 
    788788    @classmethod 
    789     def _GetNextPointCoords(cls, currentX, currentY, endX, endY, distanceToNextPoint, linesSR, unit, isTemporal): 
     789    def _GetNextPointCoords(cls, currentX, currentY, endX, endY, distanceToNextPoint, linesSR, unit, temporalInterval): 
    790790 
    791791        # If it is it is a geographic coordinate system and the unit 
     
    794794        # bearing) toward the end point. 
    795795 
    796         if not linesSR.IsProjected() and unit != 'decimal degrees' and not isTemporal: 
     796        if not linesSR.IsProjected() and unit != 'decimal degrees' and not temporalInterval: 
    797797            lat1 = math.radians(currentY) 
    798798            lat2 = math.radians(endY) 
     
    22222222u"""Output point feature class to create. 
    22232223 
    2224 If track ID or order by fields are specified, they will be added to 
    2225 this feature class. If a temporal unit is specified, a field called 
    2226 PointTime will be added to hold the points' dates and times. You may 
    2227 also specify additional fields of the lines to copy to this feature 
    2228 class (see Additional Options)."""), 
     2224By default, the tool will create a field named Distance on the feature 
     2225class and populate it with the total distance travelled from the start 
     2226of the track. The field name is specified by the Distance Travelled 
     2227Field To Create parameter (under Additional Options). If a temporal 
     2228value is specified for the Interval Unit parameter, the distance 
     2229travelled will be in the units of the underlying coordinate system: 
     2230either decimal degrees for angular (a.k.a. geographic) coordinate 
     2231systems, or the linear unit (typically meters) for projected 
     2232coordinate systems. If a non-temporal value is specified for the 
     2233Interval Unit, the distance travelled will be in that unit. 
     2234 
     2235Depending on what you specify for other parameters of the tool, 
     2236various additional fields will be created and populated."""), 
    22292237    direction = u'Output', 
    22302238    arcGISDisplayName=_(u'Output point feature class')) 
     
    23042312already, use the FID or OBJECTID field. 
    23052313 
    2306 If the input layer contains multiple tracks that each may have more 
    2307 than one line, provide the field that identifies the line."""), 
     2314If you do provide a field, it will be copied to the output points. If 
     2315you use the FID or OBJECTID field, the corresponding field of the 
     2316points will be named FID_ or OBJECTID_, to avoid conflicting with the 
     2317FID or OBJECTID of the points themselves."""), 
    23082318    arcGISDisplayName=_(u'Track ID field'), 
    23092319    arcGISParameterDependencies=[u'inputLines']) 
     
    23162326When the tool lays out points along the lines, it will first order 
    23172327them in ascending order using this field. If this field is omitted, 
    2318 the FID or OBJECTID will be used."""), 
     2328the FID or OBJECTID will be used. 
     2329 
     2330If you do provide a field, it will be copied to the output points. If 
     2331you use the FID or OBJECTID field, the corresponding field of the 
     2332points will be named FID_ or OBJECTID_, to avoid conflicting with the 
     2333FID or OBJECTID of the points themselves. (The FID or OBJECTID field 
     2334will not be copied when you do not explicitly specify it here.)"""), 
    23192335    arcGISDisplayName=_(u'Order by field'), 
    23202336    arcGISParameterDependencies=[u'inputLines']) 
     
    23272343 
    23282344This parameter must be specified if the Interval Unit is set to a 
    2329 temporal unit (i.e. Day, Hour, Minute, or Second). It is ignored if 
    2330 the Interval Unit is set to an angular or linear unit."""), 
     2345temporal unit (i.e. Day, Hour, Minute, or Second). This parameter is 
     2346optional when a non-temporal unit is used. 
     2347 
     2348By default, when this parameter and the End Time Field parameter are 
     2349both specified, two additional fields will be created on the output points: 
     2350 
     2351* PointTime - time of the point. 
     2352 
     2353* Elapsed - time elapsed since the start of the track, in hours by 
     2354  default. 
     2355 
     2356The names of these fields and the units of the Elapsed field are 
     2357specified by other parameters (see Additional Options)."""), 
    23312358    arcGISDisplayName=_(u'Start time field'), 
    23322359    arcGISParameterDependencies=[u'inputLines']) 
     
    23392366 
    23402367This parameter must be specified if the Interval Unit is set to a 
    2341 temporal unit (i.e. Day, Hour, Minute, or Second). It is ignored if 
    2342 the Interval Unit is set to an angular or linear unit."""), 
     2368temporal unit (i.e. Day, Hour, Minute, or Second). This parameter is 
     2369optional when a non-temporal unit is used."""), 
    23432370    arcGISDisplayName=_(u'End time field'), 
    23442371    arcGISParameterDependencies=[u'inputLines']) 
     
    24232450    arcGISParameterDependencies=[u'inputLines']) 
    24242451 
     2452AddArgumentMetadata(ArcGISPoints.CreatePointsAlongLines, u'distanceField', 
     2453    typeMetadata=UnicodeStringTypeMetadata(canBeNone=True), 
     2454    description=_( 
     2455u"""Point field to receive the total distance travelled from the start 
     2456of the track. 
     2457 
     2458If a temporal value is specified for the Interval Unit parameter, the 
     2459distance travelled will be in the units of the underlying coordinate 
     2460system: either decimal degrees for angular (a.k.a. geographic) 
     2461coordinate systems, or the linear unit (typically meters) for 
     2462projected coordinate systems. If a non-temporal value is specified for 
     2463the Interval Unit, the distance travelled will be in that unit. 
     2464 
     2465The field will be created with the double-precision floating point 
     2466data type."""), 
     2467    arcGISDisplayName=_(u'Distance travelled field to create')) 
     2468 
     2469AddArgumentMetadata(ArcGISPoints.CreatePointsAlongLines, u'pointTimeField', 
     2470    typeMetadata=UnicodeStringTypeMetadata(canBeNone=True), 
     2471    description=_( 
     2472u"""Point field to receive the time of the point. 
     2473 
     2474If either the Start Time Field or End Time Field parameters are not 
     2475specified, this parameter is ignored and no field will be 
     2476created."""), 
     2477    arcGISDisplayName=_(u'Time field to create')) 
     2478 
     2479AddArgumentMetadata(ArcGISPoints.CreatePointsAlongLines, u'elapsedField', 
     2480    typeMetadata=UnicodeStringTypeMetadata(canBeNone=True), 
     2481    description=_( 
     2482u"""Point field to receive the elapsed time since the start of the 
     2483track. 
     2484 
     2485If either the Start Time Field or End Time Field parameters are not 
     2486specified, this parameter is ignored and no field will be 
     2487created. 
     2488 
     2489The Elapsed Time Units parameter specifies the units of this 
     2490parameter. 
     2491 
     2492The field will be created with the double-precision floating point 
     2493data type."""), 
     2494    arcGISDisplayName=_(u'Elapsed time field to create')) 
     2495 
     2496AddArgumentMetadata(ArcGISPoints.CreatePointsAlongLines, u'elapsedUnit', 
     2497    typeMetadata=UnicodeStringTypeMetadata(canBeNone=True, makeLowercase=True, allowedValues=[u'Days', u'Hours', u'Minutes', u'Seconds']), 
     2498    description=_( 
     2499u"""Unit of the Elapsed Time Field."""), 
     2500    arcGISDisplayName=_(u'Elapsed time unit')) 
     2501 
    24252502CopyArgumentMetadata(ArcGISPoints.CreateFeatureClassWithPoints, u'overwriteExisting', ArcGISPoints.CreatePointsAlongLines, u'overwriteExisting') 
    24262503 
Note: See TracChangeset for help on using the changeset viewer.