def read_points(file, extras): """ Read points from an uploaded pnezd (comma delimited) file. """ print('Filename: %s' % file.name) print('Content-type: %s' % file.content_type) print('Character set: %s' % file.charset) print('Extras: %s' % extras) points = [] for line in [l.decode(encoding='utf-8-sig').strip() for l in file]: # skip blank lines and comment lines starting with '#' if len(line) == 0 or line[0] == '#': continue fields = line.split(',', maxsplit=4) if len(fields) != 5: raise PointsFileFormatError('Field count error: ' + line) p, n, e, z, d = fields points.append(Point( name=p, x=float(e), y=float(n), elevation=float(z), description=d )) # Add the file information and units. datafile = DataFile(file_name=file.name, file_type=DataFile.FILE_TYPE_PNEZD, owner=extras['owner'], modified=timezone.now()) datafile.save() for p in points: p.data_file = datafile p.units = extras['units'] # Point.objects.all().delete() # clear out old points Point.objects.bulk_create(points) return len(points)
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)