My girlfriend was not at home in the middle of the night and stole her whereabouts in Python. What was the result?

Keywords: Python

Target scene

Sometimes my girlfriend plays outside alone and asks where she is, but she doesn't tell me. However, you want to know the "location" of your girlfriend. What should you do?

In fact, you can do this routine for your girlfriend to pretend that you are bored at home. You can help her repair the map and ask her to send the original map to you through wechat. After you get the "original wechat map", you can quickly get the specific location of your girlfriend by using Python.

preparation

First, install a library that identifies picture metadata in the virtual environment.

 

Script

The whole operation is divided into three steps: obtaining the longitude and latitude of the picture, correcting the longitude and latitude, and calling Gaode inverse geocoding API to obtain the specific location.

Step 1, get the "longitude and latitude" of the picture.

Using exifrad library, you can directly read the picture file and obtain the metadata of the picture, including longitude, latitude, north-south latitude, east-west longitude and shooting time.

# Use exifred to get the metadata of the picture
img_exif = exifread.process_file(open(self.img_path, 'rb'))

# Ability to read properties
if img_exif:
     # Latitude number
     latitude_gps = img_exif['GPS GPSLatitude']

     # N. S north-south latitude
     latitude_direction = img_exif['GPS GPSLatitudeRef']

     # Longitude number
     longitude_gps = img_exif['GPS GPSLongitude']

     # E. W east west longitude direction
     longitude_direction = img_exif['GPS GPSLongitudeRef']

     # Shooting time
     take_time = img_exif['EXIF DateTimeOriginal']

If the metadata exists, then judge whether the shooting time is reasonable. If the shooting time is not today, I can only regret to inform you that your girlfriend is lying to you.

def judge_time_met(self, take_time):
    """
    Determine whether the shooting time is today
    :param take_time:
    :return:
    """
    # Shooting time
    format_time = str(take_time).split(" ")[0].replace(":", "-")

    # Date of the day
    today = str(datetime.date.today())

    if format_time == today:
        return False
    else:
        return True

if is_lie:
        print('I'm sorry to inform you that your girlfriend is lying!!!')
        return

If your girlfriend doesn't lie, you can proceed to step 2.

Because there are some errors in the longitude and latitude obtained by GPS and the coordinates of gaude map, it is necessary to convert the coordinates into "Mars coordinate system".

x_pi = 3.14159265358979324 * 3000.0 / 180.0
pi = 3.1415926535897932384626  # π
a = 6378245.0  # Long half axis
ee = 0.00669342162296594323  # Oblateness

def wgs84togcj02(lng, lat):
    """
    WGS84 turn GCJ02(Mars coordinate system)
    :param lng:WGS84 Longitude of coordinate system
    :param lat:WGS84 Latitude of coordinate system
    :return:
    """
    if out_of_china(lng, lat):  # Judge whether it is in China
        return lng, lat
    dlat = transformlat(lng - 105.0, lat - 35.0)
    dlng = transformlng(lng - 105.0, lat - 35.0)
    radlat = lat / 180.0 * pi
    magic = math.sin(radlat)
    magic = 1 - ee * magic * magic
    sqrtmagic = math.sqrt(magic)
    dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi)
    dlng = (dlng * 180.0) / (a / sqrtmagic * math.cos(radlat) * pi)
    mglat = lat + dlat
    mglng = lng + dlng
    return [mglng, mglat]

In addition, it should be noted that the longitude and latitude parameters in the interface can only identify 6 digits after the decimal point. It is necessary to do some data processing for the degrees, minutes and seconds in the longitude and latitude, and then round them.

def __format_lati_long_data(self, data):
    """
    Process longitude and latitude data with 6 decimal places
    :param data: Original longitude and latitude values
    :return:
    """
    # Remove the left and right parentheses and spaces
    data_list_tmp = str(data).replace('[', '').replace(']', '').split(',')
    data_list = [data.strip() for data in data_list_tmp]

    # Replace the value of seconds
    data_tmp = data_list[-1].split('/')

    # Value of seconds
    data_sec = int(data_tmp[0]) / int(data_tmp[1]) / 3600

    # Replace the value of the score
    data_tmp = data_list[-2]

    # Value of score
    data_minute = int(data_tmp) / 60

    # Value of degree
    data_degree = int(data_list[0])

    # Since Gaode API can only recognize 6 digits after the decimal point
    # It needs to be converted to floating point number and kept as 6 decimal places
    result = "%.6f" % (data_degree + data_minute + data_sec)
    return float(result)

Step 3, call Gaode's anti geocoding API, pass in the application Key, and you can get the detailed address of your girlfriend.

def __get_address(self, location):
    """
    Get the detailed address according to the coordinates
    :param location: Longitude and latitude value
    :return:
    """
    resp = requests.get(self.url_get_position.format(self.api_key, location))

    location_data = json.loads(resp.text)

    address = location_data.get('regeocode').get('formatted_address')

    return address

Result conclusion

Make sure that the picture is based on the original picture, which can quickly help you judge whether your girlfriend is lying; If your girlfriend doesn't lie, go back to her specific location.

python gift bag  

Posted by Aeiri on Wed, 24 Nov 2021 19:27:40 -0800