示例#1
0
    def __init__(self, params, units):
        self.units = units
        self.units_factor = units_factor(units)

        if params.type == ProjectionParams.TYPE_LC:
            self._projection = ProjectionLambertConic(params)
        elif params.type == ProjectionParams.TYPE_TM:
            self._projection = ProjectionTransverseMercator(params)
        else:
            raise ProjectionTypeError('Bad projection type: {0}'.format(params.type))
示例#2
0
def read_waypoints(file, extras):
    """ Read wpt elements from an uploaded gpx file and convert to grid coordinates.
    """

    print('Filename: %s' % file.name)
    print('Content-type: %s' % file.content_type)
    print('Character set: %s' % file.charset)
    print('Extras: %s' % extras)

    ns = {
        'gpx': 'http://www.topografix.com/GPX/1/1',
        'gpxx': 'http://www.garmin.com/xmlschemas/GpxExtensions/v3',
        'wpt1': 'http://www.garmin.com/xmlschemas/WaypointExtension/v1',
        'ctx': 'http://www.garmin.com/xmlschemas/CreationTimeExtension/v1'
    }

    etree.register_namespace('', 'http://www.topografix.com/GPX/1/1')
    etree.register_namespace('gpxx', 'http://www.garmin.com/xmlschemas/GpxExtensions/v3')
    etree.register_namespace('wpt1', 'http://www.garmin.com/xmlschemas/WaypointExtension/v1')
    etree.register_namespace('ctx', 'http://www.garmin.com/xmlschemas/CreationTimeExtension/v1')

    def parse_value(e, tag, default):
        t = e.find(tag, ns)
        if t is None: return default
        if type(default) is int: return int(t.text)
        if type(default) is float: return float(t.text)
        return t.text

    # Initialize the projection.
    params = ProjectionParams.objects.get(system=extras['system'], code=extras['projection_code'])
    proj = Projection(params, units=extras['units'])

    points = []
    gpx = etree.parse(file).getroot()
    for e in gpx.findall('gpx:wpt', ns):

        x, y = proj.to_grid(float(e.get('lat')), float(e.get('lon')))
        ele = parse_value(e, 'gpx:ele', 0.0) / units_factor('meters')
        name = parse_value(e, 'gpx:name', None)
        cmt = parse_value(e, 'gpx:cmt', None)
        desc = parse_value(e, 'gpx:desc', None)
        time = parse_value(e, 'gpx:time', None)
        sym = parse_value(e, 'gpx:sym', 'Waypoint')
        samples = parse_value(e, './/wpt1:Samples', 0)

        points.append(GridPoint(
            projection=params, x=x, y=y, elevation=ele, name=name, description=cmt, samples=samples,
        ))

    # Add the file information and units.
    datafile = DataFile(file_name=file.name, file_type=DataFile.FILE_TYPE_GPX,
                        owner=extras['owner'], modified=timezone.now())
    datafile.save()
    for p in points:
        p.data_file = datafile
        p.units = extras['units']

    # GridPoint.objects.all().delete()   # clear out old grid points
    GridPoint.objects.bulk_create(points)

    return len(points)