def make_polygon_string(code): mesh = parse_mesh_code(code) start_lat, start_lon, end_lat, end_lon = make_min_max_lat_lon(mesh) polygon_string = "POLYGON(({xmin} {ymin}, {xmax} {ymin}, {xmax} {ymax}, {xmin} {ymax}, {xmin} {ymin}))"\ .format(xmin=start_lat, xmax=end_lat, ymin=start_lon, ymax=end_lon) corr_list = [[[start_lat, start_lon], [end_lat, start_lon], [end_lat, end_lon], [start_lat, end_lon], [start_lat, start_lon]]] geo_json_dict = {"type": "Polygon", "coordinates": corr_list} geo_json = json.dumps(geo_json_dict) return (polygon_string, geo_json)
def lat_lon_meshcodes(in_file, out_file): lines = [] val = [] with open(in_file, 'r') as k: for line in k: lines.append(line) li = line.split() val.append(li) kq = 1 with open(out_file, 'w') as f: for i in range(len(lines)): for m in val[i]: mesh = parse_mesh_code(m) mesh_center = mesh.south_west + (mesh.size / 2.0) x = (mesh_center.lon.degree, mesh_center.lat.degree) n_w = str( (str(mesh.south_west.lon.degree) + ' ' + str(mesh.south_west.lat.degree), str(mesh.south_west.lon.degree) + ' ' + str(mesh.south_west.lat.degree + mesh.size.lat.degree), str(mesh.south_west.lon.degree + mesh.size.lon.degree) + ' ' + str(mesh.south_west.lat.degree + mesh.size.lat.degree), str(mesh.south_west.lon.degree + mesh.size.lon.degree) + ' ' + str(mesh.south_west.lat.degree), str(mesh.south_west.lon.degree) + ' ' + str(mesh.south_west.lat.degree))) # n_w.replace("'", "") n_w = 'POLYGON(' + n_w + ')' u_s = str(n_w) u_s.replace("'", "") print(u_s) f.write('%s' % m.rstrip()) f.write('\t%s' % kq) f.write('\t%s' % u_s) # f.write(' %s'%str(x)) f.write('\n') kq += 1 f.close() lin = [] line_s = [] with open(out_file, 'r') as r: for line in r: pattern = line.replace("'", "") line_s.append(pattern) r.close() with open(out_file, 'w') as f: for x in line_s: f.write('%s' % str(x)) f.close()
def test_validcode(): """ Returns correct meshes if valid mesh codes are given. """ ok_(isinstance(parse_mesh_code('5339'), FirstMesh)) ok_(isinstance(parse_mesh_code('533935'), SecondMesh)) ok_(isinstance(parse_mesh_code('53393573'), ThirdMesh)) ok_(isinstance(parse_mesh_code('533935731'), HalfMesh)) ok_(isinstance(parse_mesh_code('5339357312'), QuarterMesh)) ok_(isinstance(parse_mesh_code('53393573123'), OneEighthMesh))
def test_invalid_code(): """ Raises ValueError if invalid mesh codes are given. """ isinstance(parse_mesh_code('533935731234'), FirstMesh)
def test_empty_code(): """ Raises ValueError if empty mesh codes are given. """ isinstance(parse_mesh_code(''), FirstMesh)
mesh_lv1_list = [str(i).zfill(4) for i in range(10000)] mesh_lv2_list = [mesh_lv1 + str(i).zfill(2) \ for mesh_lv1 in mesh_lv1_list for i in range(78) if str(i).find("8") == -1 and str(i).find("9") == -1] def make_min_max_lat_lon(mesh): start_lat = mesh.south_west.lat.degree start_lon = mesh.south_west.lon.degree end_lat = mesh.south_west.lat.degree + mesh.size.lat.degree end_lon = mesh.south_west.lon.degree + mesh.size.lon.degree return (start_lat, start_lon, end_lat, end_lon) for mesh_code in mesh_lv2_list: population = random.randrange(1000000) code = mesh_code mesh = parse_mesh_code(mesh_code) start_lat, start_lon, end_lat, end_lon = make_min_max_lat_lon(mesh) data += """"{population}","{code}","{start_lat}","{start_lon}","{end_lat}","{end_lon}"\n""" \ .format(population=population, code=code, start_lat=start_lat, start_lon=start_lon, end_lat=end_lat, end_lon=end_lon) with open("mesh_csv/mesh_lv2.csv", "w") as f: f.write(data)