Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dependency on geopy #316

Merged
merged 2 commits into from
Oct 16, 2020
Merged

Remove dependency on geopy #316

merged 2 commits into from
Oct 16, 2020

Conversation

danielolsen
Copy link
Contributor

@danielolsen danielolsen commented Oct 16, 2020

Purpose

Reduce number/scope of required dependencies. geopy is "a Python client for several popular geocoding web services. geopy makes it easy for Python developers to locate the coordinates of addresses, cities, countries, and landmarks across the globe using third-party geocoders and other data sources." We are using it to calculate the distance between two points.

What is the code doing

In distance.py, great_circle_distance takes a series with {from_lat, from_lon, to_lat, to_lon} and returns the distance between the two points. As far as I can tell, it's used only from within PostREISE. I've refactored it to use the haversine function/formula instead, and deleted the geopy import and requirements.

Validation

Over our entire grid, the maximum length difference between vincenty and haversine methods is 0.4 miles, for a 211 mile line. The largest relative difference appears to be 0.4% on a zero-length line (floating point cruft?).

(run with develop):

>>> from powersimdata.input.grid import Grid
>>> from powersimdata.utility.distance import haversine
>>> from powersimdata.utility.distance import great_circle_distance
>>> usa = Grid(['USA'])
Reading bus.csv
Reading plant.csv
Reading gencost.csv
Reading branch.csv
Reading dcline.csv
Reading sub.csv
Reading bus2sub.csv
Reading zone.csv
>>> branch = usa.branch
>>> branch["haversine"] = branch.apply(lambda x: haversine((x.from_lat, x.from_lon), (x.to_lat, x.to_lon)), axis=1)
>>> branch["gcd"] = branch.apply(great_circle_distance, axis=1) / 1.60934
>>> (branch.haversine - branch.gcd).abs().max()
0.42182497103240735
>>> (branch.haversine - branch.gcd).abs().idxmax()
96179
>>> branch.loc[96179]
from_bus_id            2030399
to_bus_id              2040783
r                     0.000895
x                     0.023936
b                      9.76203
rateA                  4235.96
rateB                        0
rateC                        0
ratio                        0
angle                        0
status                       1
angmin                       0
angmax                       0
Pf                    -1071.49
Qf                     -312.61
Pt                     1081.59
Qt                     -438.85
mu_Sf                        0
mu_St                        0
mu_angmin                    0
mu_angmax                    0
branch_device_type        Line
interconnect           Western
from_zone_id               208
to_zone_id                 209
from_zone_name          Nevada
to_zone_name           Arizona
from_lat               36.0875
from_lon              -115.051
to_lat                 36.9047
to_lon                -111.389
haversine              211.077
gcd                    211.499
Name: 96179, dtype: object
>>> ((branch.haversine - branch.gcd) / branch.gcd).abs().max()
0.003719583059931294
35428
>>> branch.loc[32428]
from_bus_id                   25583
to_bus_id                     25587
r                           0.00023
x                          0.001164
b                                 0
rateA                             0
rateB                             0
rateC                             0
ratio                             0
angle                             0
status                            1
angmin                            0
angmax                            0
Pf                                0
Qf                                0
Pt                                0
Qt                                0
mu_Sf                             0
mu_St                             0
mu_angmin                         0
mu_angmax                         0
branch_device_type             Line
interconnect                Eastern
from_zone_id                     20
to_zone_id                       20
from_zone_name        Georgia South
to_zone_name          Georgia South
from_lat                    31.6505
from_lon                   -84.0929
to_lat                      31.6505
to_lon                     -84.0929
haversine                         0
gcd                               0
Name: 32428, dtype: object

Time to review

5 minutes.

site_coords = (x.from_lat, x.from_lon)
place2_coords = (x.to_lat, x.to_lon)
return geopy.distance.vincenty(site_coords, place2_coords).km
return haversine((x.from_lat, x.from_lon), (x.to_lat, x.to_lon)) * 1.60934
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use a constant here? mi_to_km = 1.60934

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Collaborator

@jenhagg jenhagg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks for cleaning this up.

Copy link
Collaborator

@rouille rouille left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for cleaning this up

@danielolsen danielolsen merged commit efe8fe3 into develop Oct 16, 2020
@danielolsen danielolsen deleted the daniel/remove_geopy branch October 16, 2020 17:18
@ahurli ahurli mentioned this pull request Mar 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants