EzDevInfo.com

geopy

Geocoding library for Python. geopy/geopy · GitHub geopy - geocoding library for python.

Normalizing street addresses in Django/Python

I have a Django form where one of the fields is a TextInput for a street address.

I want to normalize the data. For example:

>> normalize('420 East 24th St.')
'420 E. 24th Street'

>> normalize('221 Amsterdam Av')
'221 Amsterdam Ave.'

>> normalize('221 Amsterdam Avenue')
'221 Amsterdam Ave.'

Or something like that. I'm already using geopy for geocoding. Perhaps this might help?

Also: Where should I normalize? In the database model or in the clean function of the form field?


Source: (StackOverflow)

Geopy ValueError "Didn't find exactly one match" when geocoding

In Django, I have been trying to get a search field to geocode a location and spit out a list from my db sorted by distance. So far everything works except when I search for a location that Google returns multiple results form such as "ann arbor, MI". I get the ValueError "Didn't find exactly one placemark! (Found 2.)" Here is my views.py

from django.shortcuts import render_to_response
from models import CampSite
from geopy import geocoders
from django.contrib.gis.geos import *
from django.contrib.gis.measure import D
from campsites.forms import SearchForm
from django.http import HttpResponseRedirect


def results(request):
    query = request.GET['q']
    g = geocoders.Google(resource='maps')
    location, (lat, lon) = g.geocode(query) 
    pnt = fromstr("POINT(%s %s)" % (lon, lat))
    distance_from_point = {'mi':'2000'}
    results = CampSite.objects.filter(lonlat__distance_lte=(pnt,D(**distance_from_point))).distance(pnt).order_by('distance')
    return render_to_response('results.html',{'location': location, 'lat': lat, 'lon': lon, 'results':results})

The common solution I found online was to change

location, (lat, lon) = g.geocode(query)


to


location, (lat, lon) = g.geocode(query, exactly_one=False)

However, this produced the new ValueError "String or unicode input unrecognized as WKT EWKT, and HEXEWKB."

This is my first django project I'm doing outside of tutorials, so thankyou for being gentile.


Source: (StackOverflow)

Advertisements

Bearing between two points

Ive been using the geopy package , which does a great job, however some of the results i get are inconsistant or come with a relatively large displacement, i suspect that the problem resides with my bearing calculation:

def gb(x,y,center_x,center_y):
dx=x-center_x
dy=y-center_y
if ((dy>=0)and((dx>0)or(dx<0))):
    return math.degrees(math.atan2(dy,dx))
elif (dy<=0)and((dx>0)or (dx<0)):
    return (math.degrees(math.atan2(dy,dx))+360)
else:
    return (math.degrees(math.atan2(dy,dx))+360)%360

I need to calculate the bearing, s.t. center_x and center_y are the pivot. afterwards i use geopy to reverse engineer the gps coordinate:

latlon = VincentyDistance(miles=dist).destination(Point(lat1, lon1), bearing)

Can anyone point me to what might i be doing wrong?


Source: (StackOverflow)

Geopy: calculating GPS heading / bearing

First time poster here.

I am doing some data analyses on collected GPS data for a bridge inspection ROV octorotor. We have the octorotor running on [ROS] using a 3D scanning LIDAR, stereo vision, INS, and some other neat tech. I'm currently using a [ublox LEA-6T] in a similar setup as [Doug Weibel's] setup to collect raw GPS data like carrier phase, doppler shift, and satellite ephemeris. Then I use an opensource project [RTKLIB] to do some DGPS post processing with local [NOAA CORS] stations to obtain cm accuracy for better pose estimation when reconstructing the 3D point cloud of the bridge.

Anyhow, I'm using most of [scipy] to statistically verify my test results.
Specifically for this portion though, I'm just using:

  • [python-3.3]
  • [numpy]
  • [geopy]

I've been studding my positional covariance with respect to offset from my measured ground truth using geopy's handy distance function. With little massaging the arguments, I can find the distance respect to each direction depicted by each standard deviation element in the matrix; North, East, Up and the three directions between.

However, these distances are absolute and do not describe direction.
Say: positive, negative would correlate to northward or southward respectively.

I could simply use the latitude and longitude to detect polarity of direction,
But I'd like to be able to find the precise point to point bearing of the distance described instead,
As I believe a value of global heading could be useful for further applications other than my current one.

I've found someone else pose a similar question
But its seem to be assuming a great circle approximation
Where I would prefer using at least the WGS-84 ellipsoidal model, or any of the same models that can be used in geopy:
Jump to Calculating distances

Any suggestion appreciated,
-ruffsl

Sources if interested:

  • [python-3.3]: http:// www.python.org/download/releases/3.3.0/
  • [numpy]: http:// www.numpy.org/
  • [geopy]: https:// code.google.com/p/geopy/
  • [scipy]: http:// www.scipy.org/
  • [ublox LEA-6T]: http:// www.u-blox.com/en/gps-modules/u-blox-6-timing-module/lea-6t.html
  • [Doug Weibel's]: http:// diydrones.com/profiles/blogs/proof-of-concept-test-extremely-accurate-3d-velocity-measurement
  • [RTKLIB]: http:// www.rtklib.com/
  • [NOAA CORS]: http:// geodesy.noaa.gov/CORS/
  • [ROS]: http:// www.ros.org/wiki/

Source: (StackOverflow)

Django - determining if geographic coordinates are inside of an circle

Does django have anything that will look at a geographic coordinate (decimal lat/long) and determine if is inside a circle with a certain radius (let's say 100 Km)?

I have certain type of data, each has a lat/long and I would like to make a search in the database to see if that data is located inside of a circle with a specified radius size.

I could probably write something myself that will handle this but I wander if there is something written already that will handle this.


Source: (StackOverflow)

Convert between coordinate systems with GeoDjango

I'm trying to add coordinate information to my database, adding django.contrib.gis support to my app. I'm writing a south data migration that takes the addresses from the database, and asks Google for the coordinates (so far I think my best bet is to use geopy for this).

Next I need to convert the returned coordinates from WGS84:4326, Google's coordinate system, to WGS84:22186, my coordinate system.

I'm lost among the GeoDjango docs trying to find a way to do this. This far, I gather I need to do this:

gcoord = SpatialReference("4326")
mycoord = SpatialReference("22186")
trans = CoordTransform(gcoord, mycoord)

but then, I don't know how to use that CoordTransform object.. seems to be used by GDAL's data objects, but that's overkill for what I want to do..


Source: (StackOverflow)

Google geocoding: points of interest within a specified radius

After searching around it appears that many people already have the lat/long data of the geographic points of interest they are interested in reverse-geocoding.

In my scenario I know a starting location and would like to find all points of interest (mainly residences within a neighborhood) that lie within a specific radius (say, 1 mile).

The first step is simply specifying a starting point and a radius to search within but I can't seem to figure out how to do this using the the Google Geocoding API (I'm not tied to Google... just figured I'd start there).

I am currently working in python (geopy and pygeocoder) but will eventually port it to iOS.

Any pointers would be much appreciated.


Source: (StackOverflow)

Geocoding using Geopy and Python

I am trying to Geocode a CSV file that contains the name of the location and a parsed out address which includes Address number, Street name, city, zip, country. I want to use GEOPY and ArcGIS Geocodes through Geopy.I wanted to create a code that loops through my csv of 5000+ entries and gives me the latitude and longitude in separate columns in my CSV. I want to use ArcGIS Geocoding service through Geopy. Can anyone provide me with a code to get started? Thanks!

Here is my script:

import csv
from geopy.geocoders import ArcGIS


geolocator = ArcGIS() #here some parameters are needed

with open('C:/Users/v-albaut/Desktop/Test_Geo.csv', 'rb') as csvinput:
    with open('output.csv', 'w') as csvoutput:
       output_fieldnames = ['Name','Address', 'Latitude', 'Longitude']
       writer = csv.DictWriter(csvoutput, delimiter=',', fieldnames=output_fieldnames)
       reader = csv.DictReader(csvinput)
       for row in reader:
            ##here you have to replace the dict item by your csv column names
            query = ','.join(str(x) for x in (row['Name'], row['Address']))
            Address, (latitude, longitude) = geolocator.geocode(query)
            ###here is the writing section
            output_row = {}
            output_row['Name'] = Name
            output_row['Address'] = Address
            output_row['Latitude'] = Latitude
            output_row['Longitude'] =Longitude
            writer.writerow(output_row)

Source: (StackOverflow)

Python Pandas 'apply' returns series; can't convert to dataframe

OK, I'm at half-wit's end. I'm geocoding a dataframe with geopy. I've written a simple function to take an input - country name - and return the latitude and longitude. I use apply to run the function and it returns a Pandas series object. I can't seem to convert it to a dataframe. I'm sure I'm missing something obvious, but I'm new to python and still RTFMing. BTW, the geocoder function works great.

# Import libraries 
import os 
import pandas as pd 
import numpy as np
from geopy.geocoders import Nominatim

def locate(x):
    geolocator = Nominatim()
    # print(x) # debug
    try:
        #Get geocode
        location = geolocator.geocode(x, timeout=8, exactly_one=True)
        lat = location.latitude
        lon = location.longitude
    except:
        #didn't work for some reason that I really don't care about
        lat = np.nan
        lon = np.nan
   #  print(lat,lon) #debug
    return lat,  lon # Note: also tried return { 'LAT': lat, 'LON': lon }

df_geo_in = df_addr.drop_duplicates(['COUNTRY']).reset_index()    #works perfectly
df_geo_in['LAT'], df_geo_in['LON']  = df_geo_in.applymap(locate) 
# error: returns more than 2 values - default index + column with results

I also tried

df_geo_in['LAT','LON'] = df_geo_in.applymap(locate)

I get a single dataframe with no index and a single colume with the series in it.

I've tried a number of other methods, including 'applymap' :

source_cols = ['LAT','LON'] 
new_cols = [str(x) for x in source_cols]

df_geo_in = df_addr.drop_duplicates(['COUNTRY']).set_index(['COUNTRY']) 
df_geo_in[new_cols] = df_geo_in.applymap(locate)

which returned an error after a long time:

ValueError: Columns must be same length as key

I've also tried manually converting the series to a dataframe using the df.from_dict(df_geo_in) method without success.

The goal is to geocode 166 unique countries, then join it back to the 188K addresses in df_addr. I'm trying to be pandas-y in my code and not write loops if possible. But I haven't found the magic to convert series into dataframes and this is the first time I've tried to use apply.

Thanks in advance - ancient C programmer


Source: (StackOverflow)

GPS positioning with Python, geopy, shapely

I'm trying to work on the following scenario:

  • Getting polygon coordinates from google earth
  • Getting boundaries with Shapely:

    >>> polygon = Polygon([(53.349459,-6.260159),(53.349366,-6.260126),(53.349383,-6.260012),(53.349478,-6.260053),(53.349459,-6.260159)])   
    >>> polygon.bounds
    (53.349366, -6.260159, 53.349478, -6.260012)
    

    I am getting 2 coordinates, which are 2 border points on the top of my figure.

  • Getting distance with geopy

    And now I am stuck... trying to figure out:

    1. How to find 2 other border points (in the bottom)
    2. How to detect whether a user is near (e.g. 3 meters) the polygon from any side? (left, right, up, down). in this case, I need to know not only the edges border points, but also all the border points from left, right, up and down? I can calculate the distance between the user location and the polygon, but what point to from polygon to take dynamically? Can I use existing libs for this, like geopy and Shapely?

    Image


Source: (StackOverflow)

Geopy Error with Reverse Geocoding

I am using Geopy. I get the following error for the code.

I have using the same code as on https://code.google.com/p/geopy/wiki/ReverseGeocoding

from geopy import geocoders

g = geocoders.GeoNames()
(place, point) = g.geocode("Palo Alto, CA 94306")
print place
>> "Palo Alto, US 94306"
print point
>> (37.418008999999998, -122.127375)

(new_place,new_point) = g.reverse(point)
print new_place
>> 3998 Ventura Ct, Palo Alto, US 94306 
print new_point
>> (37.417850000000001, -122.12793000000001)

Works fine till print point. Error occurs with g.reverse(point)

Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Python27\lib\site-packages\geopy\geocoders\base.py", line 9, in reverse
    raise NotImplementedError
NotImplementedError

Any suggestions?


Source: (StackOverflow)

Return individual address components (city, state, etc.) from GeoPy geocoder

I'm using GeoPy to geocode addresses to lat,lng. I would also like to extract the itemized address components (street, city, state, zip) for each address.

GeoPy returns a string with the address -- but I can't find a reliable way to separate each component. For example:

123 Main Street, Los Angeles, CA 90034, USA =>
{street: '123 Main Street', city: 'Los Angeles', state: 'CA', zip: 90034, country: 'USA'}

The Google geocoding API does return these individual components... is there a way to get these from GeoPy? (or a different geocoding tool?)


Source: (StackOverflow)

How to find if lat/long falls in an area using Django and geopy

I'm trying to create a Django app that would take an inputted address and return a list of political races that person would vote in. I have maps of all the districts (PDFs). And I know that I can use geopy to convert an inputted address into coordinates. How do I define the voter districts in Django so that I can run a query to see what districts those coordinates fall in?


Source: (StackOverflow)

Most straightforward way to cache geocoding data

I am using geopy to get lat/long coordinates for a list of addresses. All the documentation points to limiting server queries by caching (many questions here, in fact), but few actually give practical solutions.

What is the best way to accomplish this?

This is for a self-contained data processing job I'm working on ... no app platform involved. Just trying to cut down on server queries as I run through data that I will have seen before (very likely, in my case).

My code looks like this:

from geopy import geocoders
def geocode( address ):
    # address ~= "175 5th Avenue NYC"
    g = geocoders.GoogleV3()
    cache = addressCached( address )

    if ( cache != False ): 
        # We have seen this exact address before,
        # return the saved location
        return cache

    # Otherwise, get a new location from geocoder
    location = g.geocode( address )

    saveToCache( address, location )
    return location

def addressCached( address ):
    # What does this look like?

def saveToCache( address, location ):
    # What does this look like?

Source: (StackOverflow)

Why is Geopy Distance.Distance wrong?

From GeoDjango Point Field, I get the following points:

object1.point = "POINT(-113.4741271000000040 53.4235217000000020)"
object2.point = "POINT(-113.5013688000000229 53.5343457999999970)"

Then I calculate the distance using geopy:

from geopy import distance
from geopy import Point

p1 = Point("-113.4741271000000040 53.4235217000000020")
p2 = Point("-113.5013688000000229 53.5343457999999970")
result = distance.distance(p1,p2).kilometers
print result
# 5.791490830933827

But using this tool: http://www.movable-type.co.uk/scripts/latlong.html I get a distance of 12.45km

Why is there such a big discrepancy?


Source: (StackOverflow)