def validate_stationing(self):
        """
        Iterate the geometry, calculating the internal start station
        based on the actual station and storing it in an
        'InternalStation' parameter tuple for the start
        and end of the curve
        """

        prev_station = self.data.get('meta').get('StartStation')
        prev_coord = self.data.get('meta').get('Start')

        if (prev_coord is None) or (prev_station is None):
            return

        for _geo in self.data.get('geometry'):

            if not _geo:
                continue

            _geo['InternalStation'] = None

            geo_station = _geo.get('StartStation')
            geo_coord = _geo.get('Start')

            #if no station is provided, try to infer it from the start
            #coordinate and the previous station
            if geo_station is None:

                geo_station = prev_station

                if not geo_coord:
                    geo_coord = prev_coord

                print(geo_coord, prev_coord)
                delta = TupleMath.length(
                    TupleMath.subtract(tuple(geo_coord), tuple(prev_coord)))

                if not support.within_tolerance(delta):
                    geo_station += delta / units.scale_factor()

                if _geo.get('Type') == 'Line':
                    _geo.start_station = geo_station
                else:
                    _geo['StartStation'] = geo_station

            prev_coord = _geo.get('End')
            prev_station = _geo.get('StartStation') \
                + _geo.get('Length')/units.scale_factor()

            int_sta = self.get_internal_station(geo_station)

            _geo['InternalStation'] = (int_sta, int_sta + _geo.get('Length'))
Esempio n. 2
0
def get_parameters(line):
    """
    Return a fully-defined line
    """

    _result = line

    if isinstance(line, dict):
        _result = Line(line)

    _coord_truth = [_result.start, _result.end]
    _param_truth = [not math.isnan(_result.bearing), _result.length > 0.0]

    #both coordinates defined
    _case_one = all(_coord_truth)

    #only one coordinate defined, plus both length and bearing
    _case_two = any(_coord_truth) \
                and not all(_coord_truth) \
                and all(_param_truth)

    if _case_one:

        line_vec = TupleMath.subtract(_result.end, _result.start)

        _length = TupleMath.length(line_vec)

        if _result.length:
            if support.within_tolerance(_result.length, _length):
                _length = _result.length

        _result.length = _length

    elif _case_two:

        _vec = \
            support.vector_from_angle(_result.bearing).multiply(_result.length)

        if _result.start:
            _result.end = TupleMath.add(_result.start, _vec)
        else:
            _result.start = TupleMath.add(_result.end, _vec)

    else:
        print('Unable to calculate parameters for line', _result)

    #result = None

    #if _case_one or _case_two:
    #    result = {**{'Type': 'Line'}, **line}

    return _result
Esempio n. 3
0
    def validate_curve_drag(self, user_data):
        """
        Validates the changes to the curves, noting errors when
        curves overlap or exceed tangent lengths
        """

        _prev_curve = None
        _curve = None
        _max = len(self.curve_trackers)

        _pp = self.drag_points[0]
        _lines = []

        for _p in self.drag_points[1:]:
            _lines.append(TupleMath.length(_p, _pp))
            _pp = _p

        for _i, _l in enumerate(_lines):

            _is_invalid = False

            #the last segment doesn't need _prev_curve
            if _i < _max:
                _curve = self.curve_trackers[_i]

            else:

                #abort if last curve has already been marked invalid
                if _curve.is_invalid:
                    return

                _prev_curve = None

            #calculate sum of tangnets
            _tan = _curve.arc.tangent

            if _prev_curve:
                _tan += _prev_curve.arc.tangent

            #curve tangents must not exceed segment length
            _is_invalid = _tan > _l

            #invalidate accordingly.
            _curve.is_invalid = _is_invalid

            if _prev_curve and not _prev_curve.is_invalid:
                _prev_curve.is_invalid = _is_invalid

            _prev_curve = _curve
    def discretize_geometry(self, interval=None, method='Segment', delta=10.0):
        """
        Discretizes the alignment geometry to a series of vector points
        interval - the starting internal station and length of curve
        method - method of discretization
        delta - discretization interval parameter
        """

        geometry = self.data.get('geometry')

        points = []
        last_curve = None

        #undefined = entire length
        if not interval:
            interval = [0.0, self.data.get('meta').get('Length')]

        #only one element = starting position
        if len(interval) == 1:
            interval.append(self.data.get('meta').get('Length'))

        #discretize each arc in the geometry list,
        #store each point set as a sublist in the main points list
        for curve in geometry:

            if not curve:
                continue

            _sta = curve.get('InternalStation')

            #skip if curve end precedes start of interval
            if _sta[1] < interval[0]:
                continue

            #skip if curve start exceeds end of interval
            if _sta[0] > interval[1]:
                continue

            _start = _sta[0]

            #if curve starts before interval, use start of interval
            if _sta[0] < interval[0]:
                _start = interval[0]

            _end = _sta[1]

            #if curve ends past the interval, stop at end of interval
            if _sta[1] > interval[1]:
                _end = interval[1]

            #calculate start position on arc and length to discretize
            _arc_int = [_start - _sta[0], _end - _start]

            #just in case, skip for zero or negative lengths
            if _arc_int[1] <= 0.0:
                continue

            if curve.get('Type') == 'Curve':

                _pts = arc.get_points(curve,
                                      size=delta,
                                      method=method,
                                      interval=_arc_int)

                if _pts:
                    points.append(_pts)

            elif curve.get('Type') == 'Spiral':

                _pts = spiral.get_points(curve, size=delta, method=method)

                if _pts:
                    points.append(_pts)

            else:

                _start_coord = line.get_coordinate(curve.get('Start'),
                                                   curve.get('BearingIn'),
                                                   _arc_int[0])

                points.append([
                    _start_coord,
                    line.get_coordinate(_start_coord, curve.get('BearingIn'),
                                        _arc_int[1])
                ])

            last_curve = curve

        #store the last point of the first geometry for the next iteration
        _prev = points[0][-1]
        result = points[0]

        if not (_prev and result):
            return None

        #iterate the point sets, adding them to the result set
        #and eliminating any duplicate points
        for item in points[1:]:

            _v = item

            #duplicate points are within a hundredth of a foot of each other
            if TupleMath.length(
                    TupleMath.subtract(tuple(_prev), tuple(item[0]))) < 0.01:

                _v = item[1:]

            result.extend(_v)
            _prev = item[-1]

        #add a line segment for the last tangent if it exists
        last_tangent = abs(
            self.data.get('meta').get('Length') \
                - last_curve.get('InternalStation')[1]
        )

        if not support.within_tolerance(last_tangent):
            _vec = TupleMath.bearing_vector(
                last_curve.get('BearingOut') * last_tangent)

            #            _vec = support.vector_from_angle(last_curve.get('BearingOut'))\
            #                .multiply(last_tangent)

            last_point = tuple(result[-1])

            result.append(TupleMath.add(last_point, _vec))

        #set the end point
        if not self.data.get('meta').get('End'):
            self.data.get('meta')['End'] = result[-1]

        return result
    def validate_coordinates(self, zero_reference):
        """
        Iterate the geometry, testing for incomplete / incorrect station /
        coordinate values. Fix them where possible, error otherwise
        """

        #calculate distance between curve start and end using
        #internal station and coordinate vectors

        _datum = self.data.get('meta')
        _geo_data = self.data.get('geometry')

        _prev_geo = {
            'End': _datum.get('Start'),
            'InternalStation': (0.0, 0.0),
            'StartStation': _datum.get('StartStation'),
            'Length': 0.0
        }

        if zero_reference:
            _prev_geo['End'] = Vector()

        for _geo in _geo_data:

            if not _geo:
                continue

            #get the vector between the two geometries
            #and the station distance
            _vector = TupleMath.subtract(tuple(_geo.get('Start')),
                                         tuple(_prev_geo.get('End')))

            _sta_len = abs(
                _geo.get('InternalStation')[0] \
                    - _prev_geo.get('InternalStation')[1]
            )

            #calculate the difference between the vector length
            #and station distance in document units
            _delta = \
                (TupleMath.length(_vector) - _sta_len) / units.scale_factor()

            #if the stationing / coordinates are out of tolerance,
            #the error is with the coordinate vector or station
            if not support.within_tolerance(_delta):
                bearing_angle = TupleMath.bearing(_vector)

                #fix station if coordinate vector bearings match
                if support.within_tolerance(bearing_angle,
                                            _geo.get('BearingIn')):


                    _int_sta = (
                        _prev_geo.get('InternalStation')[1] \
                            + TupleMath.length(_vector),
                        _geo.get('InternalStation')[0]
                        )

                    _start_sta = _prev_geo.get('StartStation') + \
                                    _prev_geo.get('Length') / \
                                        units.scale_factor() + \
                                           TupleMath.length(_vector) / \
                                               units.scale_factor()

                    _geo['InternalStation'] = _int_sta
                    _geo['StartStation'] = _start_sta

                #otherwise, fix the coordinate
                else:
                    _bearing_vector = TupleMath.multiply(
                        TupleMath.bearing_vector(_geo.get('BearingIn')),
                        _sta_len)

                    _start_pt = TupleMath.add(_prev_geo.get('End'),
                                              _bearing_vector)
                    _geo['Start'] = _start_pt

            _prev_geo = _geo
    def validate_datum(self):
        """
        Ensure the datum is valid, assuming 0+00 / (0,0,0)
        for station and coordinate where none is suplpied and it
        cannot be inferred fromt the starting geometry
        """
        _datum = self.data.get('meta')
        _geo = self.data.get('geometry')[0]

        if not _geo or not _datum:
            return

        _datum_truth = [
            not _datum.get('StartStation') is None,
            not _datum.get('Start') is None
        ]

        _geo_truth = [
            not _geo.get('StartStation') is None, not _geo.get('Start') is None
        ]

        #----------------------------
        #CASE 0
        #----------------------------
        #both defined?  nothing to do
        if all(_datum_truth):
            return

        #----------------------------
        #Parameter Initialization
        #----------------------------
        _geo_station = 0
        _geo_start = Vector()

        if _geo_truth[0]:
            _geo_station = _geo.get('StartStation')

        if _geo_truth[1]:
            _geo_start = _geo.get('Start')

        #---------------------
        #CASE 1
        #---------------------
        #no datum defined?  use initial geometry or zero defaults
        if not any(_datum_truth):

            _datum['StartStation'] = _geo_station
            _datum['Start'] = _geo_start
            return

        #--------------------
        #CASE 2
        #--------------------
        #station defined?
        #if the geometry has a station and coordinate,
        #project the start coordinate

        if _datum_truth[0]:

            _datum['Start'] = _geo_start

            #assume geometry start if no geometry station
            if not _geo_truth[0]:
                return

            #scale the distance to the system units
            delta = _geo_station - _datum['StartStation']

            #cutoff if error is below tolerance
            if not support.within_tolerance(delta):
                delta *= units.scale_factor()
            else:
                delta = 0.0

            #assume geometry start if station delta is zero
            if delta:

                #calculate the start based on station delta
                _datum['Start'] =\
                    TupleMath.subtract(_datum.get('Start'),
                    TupleMath.scale(
                        TupleMath.bearing_vector(_geo.get('BearingIn')),
                        delta)
                        #_geo.get('BearingIn')).multiply(delta)
                    )

            return

        #---------------------
        #CASE 3
        #---------------------
        #datum start coordinate is defined
        #if the geometry has station and coordinate,
        #project the start station
        _datum['StartStation'] = _geo_station

        #assume geometry station if no geometry start
        if _geo_truth[1]:

            #scale the length to the document units
            delta = TupleMath.length(
                TupleMath.subtract(
                    _geo_start, _datum.get('Start'))) / units.scale_factor()

            _datum['StartStation'] -= delta
    def validate_alignment(self):
        """
        Ensure the alignment geometry is continuous.
        Any discontinuities (gaps between end / start coordinates)
        must be filled by a completely defined line
        """

        _prev_sta = 0.0
        _prev_coord = self.data['meta']['Start']
        _geo_list = []

        #iterate through all geometry, looking for coordinate gaps
        #and filling them with line segments.
        for _geo in self.data.get('geometry'):

            if not _geo:
                continue

            _coord = _geo.get('Start')

            _d = abs(
                TupleMath.length(
                    TupleMath.subtract(tuple(_coord), tuple(_prev_coord))))

            #test for gap at start of geometry and end of previous geometry
            if not support.within_tolerance(_d, tolerance=0.01):

                #build the line using the provided parameters and add it
                _geo_list.append(
                    line.get_parameters({
                        'Start':
                        Vector(_prev_coord),
                        'End':
                        Vector(_coord),
                        'StartStation':
                        self.get_alignment_station(_prev_sta),
                        'Bearing':
                        _geo.get('BearingIn'),
                    }).to_dict())

            _geo_list.append(_geo)
            _prev_coord = _geo.get('End')
            _prev_sta = _geo.get('InternalStation')[1]

        _length = 0.0

        #fill in the alignment length.  If the end of the alignment falls short
        #of the calculated length, append a line to complete it.
        if not self.data.get('meta').get('Length'):

            _end = self.data.get('meta').get('End')

            #abort - no overall length or end coordinate specified
            if not _end:
                return False

            _prev = _geo_list[-1]

            if TupleMath.length(TupleMath.subtract(_end,
                                                   _prev.get('End'))) > 0.0:

                _geo_list.append(
                    line.get_parameters({
                        'Start':
                        _prev.get('End'),
                        'End':
                        _end,
                        'StartStation':
                        self.get_alignment_station(
                            _prev['InternalStation'][0]),
                        'Bearing':
                        _prev.get('BearingOut')
                    }).to_dict())

            self.data.get('meta')['Length'] = 0.0

        #accumulate length across individual geometry and test against
        #total alignment length
        for _geo in _geo_list:
            _length += _geo.get('Length')

        align_length = self.data.get('meta').get('Length')

        if not support.within_tolerance(_length, align_length):

            if _length > align_length:
                self.data.get('meta')['Length'] = align_length

            else:
                _start = _geo_list[-1].get('End')
                bearing = _geo_list[-1].get('BearingOut')

                _end = line.get_coordinate(_start, bearing,
                                           align_length - _length)

                _geo_list.append(
                    line.get_parameters({
                        'Start':
                        _start,
                        'End':
                        _end,
                        'StartStation':
                        self.get_alignment_station(_geo['InternalStation'][1]),
                        'BearingOut':
                        bearing
                    }).to_dict())

        self.data['geometry'] = _geo_list

        return True