def load_projections(file, extras): """ Load the projections table from an uploaded csv file. """ # print('Filename: %s' % file.name) # print('Content-type: %s' % file.content_type) # print('Character set: %s' % file.charset) # Get a list of the valid coordinate systems and projection types. COORD_SYSTEMS = [s[0] for s in ProjectionParams.SYSTEM_CHOICES] PROJECTION_TYPES = [t[0] for t in ProjectionParams.TYPE_CHOICES] projections = [] 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 params = line.split(',') if len(params) != 11: raise ProjectionFileFormatError('Field count error: ' + line) system, code, description, type = params[0:4] if system not in COORD_SYSTEMS: raise ProjectionFileFormatError('Bad coordinate system \'%s\': %s' % (system, line)) if type not in PROJECTION_TYPES: raise ProjectionFileFormatError('Bad projection type \'%s\': %s' % (type, line)) proj = ProjectionParams( system=system, code=code, description=description, type=type ) # Defining coordinate system constants for the zone proj.P0 = dms_radians(params[4]) proj.M0 = dms_radians(params[5]) proj.X0 = float(params[6]) proj.Y0 = float(params[7]) # The following projection-specific constants may be empty depending on the projection type. proj.P1 = dms_radians(params[8]) if params[8] != '' else None proj.P2 = dms_radians(params[9]) if params[9] != '' else None proj.K0 = 1.0 - 1.0/float(params[10]) if params[10] != '' else None projections.append(proj) # Clear out old projections and load new projections. ProjectionParams.objects.all().delete() ProjectionParams.objects.bulk_create(projections) return len(projections)
def test_degree_conversions(self): """ Signed integers are taken to be degrees. """ self.assertEqual(dms_radians('0'), 0.0) self.assertEqual(dms_radians('5'), math.radians(5.0)) self.assertEqual(dms_radians('-5'), math.radians(-5.0)) self.assertEqual(dms_radians('725'), math.radians(725.0)) self.assertEqual(dms_radians('-725'), math.radians(-725.0)) # Format errors. self.assertRaises(DmsFormatError, dms_radians, '') self.assertRaises(DmsFormatError, dms_radians, '-')
def test_second_conversions(self): """ Signed integers are taken to be degrees. """ self.assertEqual(dms_radians('0-0-0'), 0.0) self.assertEqual(dms_radians('0-00-00'), 0.0) self.assertEqual(dms_radians('0-00-00.00'), 0.0) self.assertEqual(dms_radians('0-00-01'), math.radians(1.0/60.0/60.0)) self.assertEqual(dms_radians('-5-01-01'), math.radians(-1.0 * (5.0 + 1.0/60.0 + 1.0/60.0/60.0))) self.assertEqual(dms_radians('5-12-34.56'), math.radians(5.0 + 12.0/60.0 + 34.56/60.0/60.0)) self.assertEqual(dms_radians('-365-59-59.99'), math.radians(-1 * (365.0 + 59.0/60.0 + 59.99/60.0/60.0))) # Format/value errors. self.assertRaises(DmsFormatError, dms_radians, '1-12-') self.assertRaises(DmsFormatError, dms_radians, '1-12-.') self.assertRaises(DmsFormatError, dms_radians, '1-12-0.') self.assertRaises(DmsFormatError, dms_radians, '1-12-60.00')
def test_minute_conversions(self): """ Signed integers are taken to be degrees. """ self.assertEqual(dms_radians('0-0'), 0.0) self.assertEqual(dms_radians('0-00'), 0.0) self.assertEqual(dms_radians('0-01'), math.radians(1.0/60.0)) self.assertEqual(dms_radians('5-01'), math.radians(5.0 + 1.0/60.0)) self.assertEqual(dms_radians('5-12'), math.radians(5.0 + 12.0/60.0)) self.assertEqual(dms_radians('-5-12'), math.radians(-1 * (5.0 + 12.0/60.0))) # Format/value errors. self.assertRaises(DmsFormatError, dms_radians, '1 12') self.assertRaises(DmsFormatError, dms_radians, '-1-') self.assertRaises(DmsFormatError, dms_radians, '1-012') self.assertRaises(DmsFormatError, dms_radians, '1-60') self.assertRaises(DmsFormatError, dms_radians, '1-59.1')