首页 > 代码库 > python脚本 随机定位坐标

python脚本 随机定位坐标



import math
import random


def rad(dg):
    return (dg * math.pi / 180)


def deg(rd):
    return (rd * 180 / math.pi)


def normalizeLongitude(lon):
    n = math.pi
    if (lon > n):
        lon = lon - 2 * n
    elif (lon < -n):
        lon = lon + 2 * n
    return lon


def isNumeric(s, mn, mx, allowNull):
    result = True
    if (s == "" and not allowNull):
        return False
    else:
        if ((float(s) != s) or (s < mn) or (s > mx)):
            return False
    return result


def calc_circle(startlat, startlon, points_num, maxdist, distunits=‘KM‘):

    assert isinstance(startlat, float), ‘startlat must be float type‘
    assert isinstance(startlon, float), ‘startlon must be float type‘

    circum_miles = 12440.883
    circum_km = 20020.732

    p = points_num

    # startlat = latLonToDecimal(startlat_value, 1, "latitude")
    if startlat == -999:
        return

    # gStartlat = startlat
    brg = [0, 180, 0]
    j = 0
    if (startlat == 90):
        startlat = 89.99999999
        j = 1

    if (startlat == -90):
        startlat = -89.99999999
        j = 2

    startlat = rad(startlat)
    if (startlon == -999):
        return

    startlon = rad(startlon)
    radius_earth = 0
    if (distunits == ‘km‘):
        mx = circum_km
        radius_earth = 6372.796924
    else:
        mx = circum_miles
        radius_earth = 3960.056052

    if (isNumeric(maxdist, 0, mx, False) is False):
        raise Exception("The max distance must be a valid number between 0 and " + mx)

    maxdist = maxdist / radius_earth

    cosdif = math.cos(maxdist) - 1
    sinstartlat = math.sin(startlat)
    cosstartlat = math.cos(startlat)

    dist = 0

    rad360 = 2 * math.pi

    output_arr = []

    for i in range(p):
        dist = math.acos((random.random() * cosdif + 1))

        brg[0] = rad360 * random.random()

        lat = math.asin(sinstartlat * math.cos(dist) + cosstartlat * math.sin(dist) * math.cos(brg[0]))

        lon = deg(normalizeLongitude(startlon * 1 + math.atan2(math.sin(brg[0]) * math.sin(dist) * cosstartlat, math.cos(dist) - sinstartlat * math.sin(lat))))

        lat = deg(lat)

        dist = round(dist * radius_earth * 10000) / 10000

        brg[0] = round(deg(brg[0]) * 1000) / 1000

        # print(‘lat‘, lat, ‘lng‘, lon, dist, brg[j])
        output_arr.append((lat, lon, dist, brg[j]))

    return output_arr


_locations = [
    ("北京市", 116.405289, 39.904987),
    ("天津市", 117.190186, 39.125595),
    ("石家庄市", 114.502464, 38.045475),
    ("唐山市", 118.175392, 39.635113),
    ("秦皇岛市", 119.586578, 39.942532),
    ("邯郸市", 114.490685, 36.612274),
    ("邢台市", 114.50885, 37.068199),
    ("保定市", 115.48233, 38.867657),
    ("张家口市", 114.884094, 40.811901),
    ("承德市", 117.939156, 40.976204),
    ("沧州市", 116.85746, 38.310581),
    ("廊坊市", 116.704437, 39.523926),
    ("衡水市", 115.665993, 37.735096),
    ("太原市", 112.549248, 37.857014),
    ("大同市", 113.295258, 40.090309),
    ("阳泉市", 113.583282, 37.861187),
    ("长治市", 113.113556, 36.191113),
    ("晋城市", 112.851273, 35.497555),
    ("朔州市", 112.433388, 39.331261),
    ("晋中市", 112.736465, 37.696495),
    ("运城市", 111.00396, 35.022778),
    ("忻州市", 112.733536, 38.41769),
    ("临汾市", 111.517975, 36.084148),
    ("吕梁市", 111.134338, 37.524364),
    ("呼和浩特市", 111.670799, 40.81831),
    ("包头市", 109.840408, 40.658169),
    ("乌海市", 106.825562, 39.673733),
    ("赤峰市", 118.956802, 42.275318),
    ("通辽市", 122.263123, 43.617428),
    ("鄂尔多斯市", 109.990288, 39.817181),
    ("呼伦贝尔市", 119.758171, 49.215332),
    ("巴彦淖尔市", 107.416962, 40.757401),
    ("乌兰察布市", 113.11454, 41.034126),
    ("兴安盟", 122.07032, 46.076267),
    ("锡林郭勒盟", 116.090996, 43.944019),
    ("阿拉善盟", 105.706421, 38.844814),
    ("沈阳市", 123.429092, 41.796768),
    ("大连市", 121.618622, 38.914589),
    ("鞍山市", 122.995628, 41.110626),
    ("抚顺市", 123.921112, 41.875957),
    ("本溪市", 123.770515, 41.297909),
    ("丹东市", 124.383041, 40.124294),
    ("锦州市", 121.135742, 41.11927),
    ("营口市", 122.235153, 40.667431),
    ("阜新市", 121.648964, 42.011795),
    ("辽阳市", 123.181519, 41.269402),
    ("盘锦市", 122.069572, 41.124485),
    ("铁岭市", 123.844276, 42.290585),
    ("朝阳市", 120.45118, 41.576759),
    ("葫芦岛市", 120.856392, 40.755573),
    ("长春市", 125.324501, 43.886841),
    ("吉林市", 126.553017, 43.843578),
    ("四平市", 124.370789, 43.170345),
    ("辽源市", 125.145348, 42.902691),
    ("通化市", 125.936501, 41.721176),
    ("白山市", 126.427841, 41.942505),
    ("松原市", 124.823608, 45.118244),
    ("白城市", 122.84111, 45.619026),
    ("延边朝鲜族自治州", 129.513229, 42.904823),
    ("哈尔滨市", 126.642464, 45.756966),
    ("齐齐哈尔市", 123.957916, 47.342079),
    ("鸡西市", 130.975967, 45.300045),
    ("鹤岗市", 130.277481, 47.332085),
    ("双鸭山市", 131.157303, 46.64344),
    ("大庆市", 125.112717, 46.590733),
    ("伊春市", 128.899399, 47.724773),
    ("佳木斯市", 130.361633, 46.809605),
    ("七台河市", 131.015579, 45.771267),
    ("牡丹江市", 129.618607, 44.582962),
    ("黑河市", 127.499023, 50.249584),
    ("绥化市", 126.992928, 46.637394),
    ("大兴安岭地区", 124.711525, 52.335262),
    ("上海市", 121.472641, 31.231707),
    ("南京市", 118.76741, 32.041546),
    ("无锡市", 120.301666, 31.57473),
    ("徐州市", 117.184814, 34.261791),
    ("常州市", 119.946976, 31.772753),
    ("苏州市", 120.619583, 31.299379),
    ("南通市", 120.864609, 32.016212),
    ("连云港市", 119.178818, 34.600018),
    ("淮安市", 119.021263, 33.597507),
    ("盐城市", 120.139999, 33.377632),
    ("扬州市", 119.421005, 32.393158),
    ("镇江市", 119.452751, 32.204403),
    ("泰州市", 119.915176, 32.484882),
    ("宿迁市", 118.275162, 33.963009),
    ("杭州市", 120.15358, 30.287458),
    ("宁波市", 121.549789, 29.868387),
    ("温州市", 120.672112, 28.000574),
    ("嘉兴市", 120.750862, 30.762653),
    ("湖州市", 120.102402, 30.867199),
    ("绍兴市", 120.582115, 29.997116),
    ("金华市", 119.649506, 29.089523),
    ("衢州市", 118.872627, 28.941708),
    ("舟山市", 122.106865, 30.016027),
    ("台州市", 121.428596, 28.661379),
    ("丽水市", 119.921783, 28.451994),
    ("合肥市", 117.283043, 31.861191),
    ("芜湖市", 118.37645, 31.326319),
    ("蚌埠市", 117.363228, 32.939667),
    ("淮南市", 117.018326, 32.647575),
    ("马鞍山市", 118.507904, 31.689362),
    ("淮北市", 116.794662, 33.971706),
    ("铜陵市", 117.816574, 30.929935),
    ("安庆市", 117.043549, 30.508829),
    ("黄山市", 118.317322, 29.709238),
    ("滁州市", 118.316261, 32.303627),
    ("阜阳市", 115.819733, 32.896969),
    ("宿州市", 116.984085, 33.633892),
    ("六安市", 116.507675, 31.75289),
    ("亳州市", 115.782936, 33.869339),
    ("池州市", 117.489159, 30.656036),
    ("宣城市", 118.757996, 30.945667),
    ("福州市", 119.306236, 26.075302),
    ("厦门市", 118.110222, 24.490475),
    ("莆田市", 119.007561, 25.431011),
    ("三明市", 117.635002, 26.265444),
    ("泉州市", 118.589424, 24.908854),
    ("漳州市", 117.661804, 24.510897),
    ("南平市", 118.178459, 26.635628),
    ("龙岩市", 117.029778, 25.091602),
    ("宁德市", 119.527084, 26.659241),
    ("南昌市", 115.892151, 28.676493),
    ("景德镇市", 117.214661, 29.292561),
    ("萍乡市", 113.852188, 27.622946),
    ("九江市", 115.992813, 29.712034),
    ("新余市", 114.930832, 27.810835),
    ("鹰潭市", 117.033836, 28.238638),
    ("赣州市", 114.940277, 25.850969),
    ("吉安市", 114.986374, 27.111698),
    ("宜春市", 114.391136, 27.8043),
    ("抚州市", 116.358353, 27.98385),
    ("上饶市", 117.971184, 28.44442),
    ("济南市", 117.000923, 36.675808),
    ("青岛市", 120.355171, 36.082981),
    ("淄博市", 118.047646, 36.814938),
    ("枣庄市", 117.557961, 34.856422),
    ("东营市", 118.664711, 37.434563),
    ("烟台市", 121.39138, 37.539295),
    ("潍坊市", 119.107079, 36.709251),
    ("济宁市", 116.587242, 35.415394),
    ("泰安市", 117.129066, 36.194969),
    ("威海市", 122.116394, 37.509689),
    ("日照市", 119.461205, 35.428589),
    ("莱芜市", 117.677734, 36.214397),
    ("临沂市", 118.326447, 35.065281),
    ("德州市", 116.307426, 37.453968),
    ("聊城市", 115.98037, 36.456013),
    ("滨州市", 118.016975, 37.383541),
    ("菏泽市", 115.469383, 35.246532),
    ("郑州市", 113.665413, 34.757977),
    ("开封市", 114.341446, 34.79705),
    ("洛阳市", 112.434471, 34.66304),
    ("平顶山市", 113.307716, 33.735241),
    ("安阳市", 114.352486, 36.103443),
    ("鹤壁市", 114.295441, 35.748238),
    ("新乡市", 113.883987, 35.302616),
    ("焦作市", 113.238266, 35.23904),
    ("济源市", 112.59005, 35.090378),
    ("濮阳市", 115.041298, 35.768234),
    ("许昌市", 113.826065, 34.022957),
    ("漯河市", 114.026405, 33.575855),
    ("三门峡市", 111.194099, 34.777336),
    ("南阳市", 112.540916, 32.999081),
    ("商丘市", 115.650497, 34.437054),
    ("信阳市", 114.075027, 32.123276),
    ("周口市", 114.649651, 33.620358),
    ("驻马店市", 114.024734, 32.980167),
    ("武汉市", 114.298569, 30.584354),
    ("黄石市", 115.077049, 30.220074),
    ("十堰市", 110.787918, 32.646908),
    ("宜昌市", 111.29084, 30.702637),
    ("襄阳市", 112.14415, 32.042427),
    ("鄂州市", 114.890594, 30.396536),
    ("荆门市", 112.204254, 31.035419),
    ("孝感市", 113.926659, 30.926422),
    ("荆州市", 112.238129, 30.326857),
    ("黄冈市", 114.879364, 30.447712),
    ("咸宁市", 114.328964, 29.832798),
    ("随州市", 113.373772, 31.717497),
    ("恩施土家族苗族自治州", 109.486992, 30.283113),
    ("仙桃市", 113.453972, 30.364952),
    ("潜江市", 112.896866, 30.421215),
    ("天门市", 113.165863, 30.653061),
    ("神农架林区", 114.298569, 30.584354),
    ("长沙市", 112.982277, 28.19409),
    ("株洲市", 113.151733, 27.835806),
    ("湘潭市", 112.944054, 27.829729),
    ("衡阳市", 112.607697, 26.900358),
    ("邵阳市", 111.469231, 27.237843),
    ("岳阳市", 113.132858, 29.370291),
    ("常德市", 111.691345, 29.040224),
    ("张家界市", 110.479919, 29.127401),
    ("益阳市", 112.355042, 28.570066),
    ("郴州市", 113.032066, 25.793589),
    ("永州市", 111.608017, 26.434517),
    ("怀化市", 109.978241, 27.550081),
    ("娄底市", 112.008499, 27.728136),
    ("湘西土家族苗族自治州", 109.739738, 28.314297),
    ("广州市", 113.28064, 23.125177),
    ("韶关市", 113.591545, 24.801323),
    ("深圳市", 114.085945, 22.547001),
    ("珠海市", 113.553986, 22.224979),
    ("汕头市", 116.708466, 23.371019),
    ("佛山市", 113.122719, 23.028763),
    ("江门市", 113.09494, 22.590431),
    ("湛江市", 110.364975, 21.274899),
    ("茂名市", 110.919228, 21.659752),
    ("肇庆市", 112.472527, 23.051546),
    ("惠州市", 114.412598, 23.079405),
    ("梅州市", 116.117584, 24.299112),
    ("汕尾市", 115.364235, 22.774485),
    ("河源市", 114.6978, 23.746265),
    ("阳江市", 111.975105, 21.859222),
    ("清远市", 113.051224, 23.685022),
    ("东莞市", 113.746262, 23.046238),
    ("中山市", 113.382393, 22.521112),
    ("东沙群岛", 112.552948, 21.810463),
    ("潮州市", 116.632301, 23.661701),
    ("揭阳市", 116.355736, 23.543777),
    ("云浮市", 112.044441, 22.929802),
    ("南宁市", 108.320007, 22.82402),
    ("柳州市", 109.411705, 24.314617),
    ("桂林市", 110.299118, 25.274216),
    ("梧州市", 111.297607, 23.474804),
    ("北海市", 109.119255, 21.473343),
    ("防城港市", 108.345474, 21.614632),
    ("钦州市", 108.624176, 21.967127),
    ("贵港市", 109.602142, 23.093599),
    ("玉林市", 110.154396, 22.631359),
    ("百色市", 106.616287, 23.897741),
    ("贺州市", 111.552055, 24.414141),
    ("河池市", 108.062103, 24.695898),
    ("来宾市", 109.229774, 23.733767),
    ("崇左市", 107.353928, 22.404108),
    ("海口市", 110.331192, 20.031971),
    ("三亚市", 109.50827, 18.247871),
    ("三沙市", 112.348824, 16.831039),
    ("五指山市", 109.516663, 18.77692),
    ("琼海市", 110.466782, 19.246012),
    ("儋州市", 109.576782, 19.517487),
    ("文昌市", 110.753975, 19.612986),
    ("万宁市", 110.388794, 18.796215),
    ("东方市", 108.653786, 19.10198),
    ("定安县", 110.349236, 19.684965),
    ("屯昌县", 110.102776, 19.362917),
    ("澄迈县", 110.007149, 19.737095),
    ("临高县", 109.687698, 19.908293),
    ("白沙黎族自治县", 109.452606, 19.224585),
    ("昌江黎族自治县", 109.053352, 19.260967),
    ("乐东黎族自治县", 109.175446, 18.74758),
    ("陵水黎族自治县", 110.037216, 18.505007),
    ("保亭黎族苗族自治县", 109.702454, 18.636372),
    ("琼中黎族苗族自治县", 109.839996, 19.03557),
    ("重庆市", 106.504959, 29.533155),
    ("成都市", 104.065735, 30.659462),
    ("自贡市", 104.773445, 29.352764),
    ("攀枝花市", 101.716003, 26.580446),
    ("泸州市", 105.443352, 28.889137),
    ("德阳市", 104.398651, 31.127991),
    ("绵阳市", 104.741722, 31.46402),
    ("广元市", 105.829758, 32.433666),
    ("遂宁市", 105.571327, 30.513311),
    ("内江市", 105.066139, 29.58708),
    ("乐山市", 103.761261, 29.582024),
    ("南充市", 106.082977, 30.79528),
    ("眉山市", 103.831787, 30.048319),
    ("宜宾市", 104.630821, 28.760189),
    ("广安市", 106.633369, 30.456398),
    ("达州市", 107.502258, 31.209484),
    ("雅安市", 103.00103, 29.987722),
    ("巴中市", 106.75367, 31.858809),
    ("资阳市", 104.641914, 30.122211),
    ("阿坝藏族羌族自治州", 102.221375, 31.899792),
    ("甘孜藏族自治州", 101.963814, 30.050663),
    ("凉山彝族自治州", 102.258743, 27.886763),
    ("贵阳市", 106.713478, 26.578342),
    ("六盘水市", 104.846741, 26.584642),
    ("遵义市", 106.937263, 27.706627),
    ("安顺市", 105.93219, 26.245544),
    ("铜仁市", 109.191551, 27.718346),
    ("黔西南布依族苗族自治州", 104.897972, 25.08812),
    ("毕节市", 105.285011, 27.301693),
    ("黔东南苗族侗族自治州", 107.977486, 26.583351),
    ("黔南布依族苗族自治州", 107.517159, 26.258219),
    ("昆明市", 102.71225, 25.040609),
    ("曲靖市", 103.797852, 25.501556),
    ("玉溪市", 102.543907, 24.35046),
    ("保山市", 99.16713, 25.111801),
    ("昭通市", 103.717216, 27.337),
    ("丽江市", 100.233025, 26.872108),
    ("普洱市", 100.972343, 22.777321),
    ("临沧市", 100.086967, 23.886566),
    ("楚雄彝族自治州", 101.546043, 25.041988),
    ("红河哈尼族彝族自治州", 103.384186, 23.366776),
    ("文山壮族苗族自治州", 104.244011, 23.369511),
    ("西双版纳傣族自治州", 100.797943, 22.001724),
    ("大理白族自治州", 100.22567, 25.589449),
    ("德宏傣族景颇族自治州", 98.578362, 24.436693),
    ("怒江傈僳族自治州", 98.854301, 25.850948),
    ("迪庆藏族自治州", 99.706467, 27.826853),
    ("拉萨市", 91.13221, 29.66036),
    ("昌都地区", 97.178452, 31.136875),
    ("山南地区", 91.766525, 29.236023),
    ("日喀则地区", 88.885147, 29.267519),
    ("那曲地区", 92.060211, 31.476004),
    ("阿里地区", 80.105499, 32.503185),
    ("林芝地区", 94.36235, 29.654694),
    ("西安市", 108.948021, 34.263161),
    ("铜川市", 108.979607, 34.91658),
    ("宝鸡市", 107.144867, 34.369316),
    ("咸阳市", 108.705116, 34.333439),
    ("渭南市", 109.502884, 34.499382),
    ("延安市", 109.490807, 36.596539),
    ("汉中市", 107.028618, 33.077667),
    ("榆林市", 109.741196, 38.290161),
    ("安康市", 109.029274, 32.6903),
    ("商洛市", 109.939774, 33.86832),
    ("兰州市", 103.823555, 36.058041),
    ("嘉峪关市", 98.277306, 39.78653),
    ("金昌市", 102.187889, 38.514236),
    ("白银市", 104.173607, 36.545681),
    ("天水市", 105.724998, 34.578529),
    ("武威市", 102.634697, 37.929996),
    ("张掖市", 100.455475, 38.932896),
    ("平凉市", 106.684692, 35.542789),
    ("酒泉市", 98.510796, 39.744022),
    ("庆阳市", 107.638374, 35.734219),
    ("定西市", 104.626297, 35.579578),
    ("陇南市", 104.929382, 33.388599),
    ("临夏回族自治州", 103.212006, 35.599445),
    ("甘南藏族自治州", 102.911011, 34.986355),
    ("西宁市", 101.778915, 36.623177),
    ("海东市", 102.103271, 36.502914),
    ("海北藏族自治州", 100.901062, 36.959435),
    ("黄南藏族自治州", 102.019989, 35.517742),
    ("海南藏族自治州", 100.619545, 36.280354),
    ("果洛藏族自治州", 100.242142, 34.473598),
    ("玉树藏族自治州", 97.008522, 33.004047),
    ("海西蒙古族藏族自治州", 97.370789, 37.374664),
    ("银川市", 106.278175, 38.46637),
    ("石嘴山市", 106.376175, 39.013329),
    ("吴忠市", 106.199409, 37.986164),
    ("固原市", 106.28524, 36.004562),
    ("中卫市", 105.189568, 37.51495),
    ("乌鲁木齐市", 87.617729, 43.792816),
    ("克拉玛依市", 84.873947, 45.595886),
    ("吐鲁番地区", 89.184074, 42.947613),
    ("哈密地区", 93.513161, 42.833248),
    ("昌吉回族自治州", 87.304008, 44.014576),
    ("博尔塔拉蒙古自治州", 82.074776, 44.903259),
    ("巴音郭楞蒙古自治州", 86.15097, 41.768551),
    ("阿克苏地区", 80.265068, 41.170712),
    ("克孜勒苏柯尔克孜自治州", 76.172829, 39.713432),
    ("喀什地区", 75.989136, 39.467663),
    ("和田地区", 79.925331, 37.110687),
    ("伊犁哈萨克自治州", 81.317947, 43.92186),
    ("塔城地区", 82.985733, 46.7463),
    ("阿勒泰地区", 88.139633, 47.848392),
    ("石河子市", 86.041077, 44.305885),
    ("阿拉尔市", 81.285881, 40.541916),
    ("图木舒克市", 79.07798, 39.867317),
    ("五家渠市", 87.526886, 44.1674),
    ("台北市", 121.509064, 25.044333),
    ("高雄市", 121.509064, 25.044333),
    ("台南市", 121.509064, 25.044333),
    ("台中市", 121.509064, 25.044333),
    ("金门县", 121.509064, 25.044333),
    ("南投县", 121.509064, 25.044333),
    ("基隆市", 121.509064, 25.044333),
    ("新竹市", 121.509064, 25.044333),
    ("嘉义市", 121.509064, 25.044333),
    ("新北市", 121.509064, 25.044333),
    ("宜兰县", 121.509064, 25.044333),
    ("新竹县", 121.509064, 25.044333),
    ("桃园县", 121.509064, 25.044333),
    ("苗栗县", 121.509064, 25.044333),
    ("彰化县", 121.509064, 25.044333),
    ("嘉义县", 121.509064, 25.044333),
    ("云林县", 121.509064, 25.044333),
    ("屏东县", 121.509064, 25.044333),
    ("台东县", 121.509064, 25.044333),
    ("花莲县", 121.509064, 25.044333),
    ("澎湖县", 121.509064, 25.044333),
    ("连江县", 121.509064, 25.044333),
    ("香港岛", 114.173355, 22.320047),
    ("九龙", 114.173355, 22.320047),
    ("新界", 114.173355, 22.320047),
    ("澳门半岛", 113.549133, 22.198751),
    ("离岛", 113.549088, 22.198952)

]


def get_random_loc(maxdist=5, areaname=‘‘):

    location = None
    if areaname:
        results = [_ for _ in _locations if _[0].find(areaname) > -1]

        if not results:
            return
        location = results[0]
    else:

        location = random.choice(_locations)

    center_point = (location[2], location[1])

    points = calc_circle(center_point[0], center_point[1], points_num=1, maxdist=maxdist)

    rand_point = (points[0][0], points[0][1])
    return rand_point


def main():
    #
    # center_point = (23.153452, 113.262816)

    location = random.choice(_locations)

    print(‘choice‘, location)

    center_point = (location[2], location[1])

    points = calc_circle(center_point[0], center_point[1], points_num=10, maxdist=5)

    def view_map(center_point, points):

        url = ‘http://www.geomidpoint.com/random/viewmap.html‘

        a1 = [str(center_point[0])]
        a2 = [str(center_point[1])]

        for point in points:
            a1.append(str(point[0]))
            a2.append(str(point[1]))

        return url + ‘?‘ + ‘|‘.join(a1) + ‘&‘ + ‘|‘.join(a2) + ‘&1‘

    for point in points:
        print(‘point‘, point)

    print(‘map view:‘, view_map(center_point, points))

    print(‘random point‘, get_random_loc(areaname=location[0]))


if __name__ == ‘__main__‘:

    main()

python脚本 随机定位坐标