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

checks geodataframe for crs before conversion #1459

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

Azaya89
Copy link
Contributor

@Azaya89 Azaya89 commented Nov 25, 2024

@Azaya89 Azaya89 self-assigned this Nov 25, 2024
Copy link

codecov bot commented Nov 25, 2024

Codecov Report

Attention: Patch coverage is 88.88889% with 2 lines in your changes missing coverage. Please review.

Project coverage is 88.96%. Comparing base (9078378) to head (1e15591).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
hvplot/tests/testgeowithoutgv.py 88.23% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1459      +/-   ##
==========================================
+ Coverage   88.94%   88.96%   +0.01%     
==========================================
  Files          52       52              
  Lines        7781     7799      +18     
==========================================
+ Hits         6921     6938      +17     
- Misses        860      861       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@maximlt maximlt left a comment

Choose a reason for hiding this comment

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

So we don't automatically project with tiles=True when the data is:

  • a lazy dataset
  • a spatialpandas object

If think we need to update the docs to mention the second item, the first one is already documented.

image

Side note but I think I just realized that GeoDataFrame means:

  • Geometry DataFrame for spatialpandas
  • GeoSpatial DataFrame for geopandas

Could that be true? :)

@Azaya89
Copy link
Contributor Author

Azaya89 commented Nov 26, 2024

So we don't automatically project with tiles=True when the data is:

  • a spatialpandas object

We do project when tiles=True if data is a spatialpandas object. See test in L101:

        bk_plot = bk_renderer.get_plot(plot)
        assert bk_plot.projection == 'mercator'  # projection enabled due to `tiles=True`

Side note but I think I just realized that GeoDataFrame means:

  • Geometry DataFrame for spatialpandas
  • GeoSpatial DataFrame for geopandas

Could that be true? :)

I think so. See https://github.com/holoviz/hvplot/blob/main/hvplot/util.py#L496

@Azaya89 Azaya89 requested a review from maximlt November 26, 2024 11:15
@maximlt
Copy link
Member

maximlt commented Nov 26, 2024

We do project when tiles=True if data is a spatialpandas object. See test in L101:

@Azaya89, no :) The data is presented on a map with Mercator coordinates but hvPlot doesn't convert the coordinates to Mercator, so they all go on vacation to the null island! I guess that, when the data is a spatialpandas object, we should apply the same logic as for a standard Pandas DataFrame object. This is where is_geodataframe is a bit misleading, as far as I can see spatialpandas objects don't contain any geospatial metadata (e.g. CRS) contrary to geopandas objects.

image
Full Code

import geopandas as gpd
import spatialpandas as spd
import hvplot.pandas

# hack to get spatialpandas to work (already fixed, not in a final release yet)
import numpy as np
np.VisibleDeprecationWarning = np.exceptions.VisibleDeprecationWarning

data = {
    'City': ['London', 'Paris', 'Berlin', 'Madrid', 'Rome', 'Vienna', 'Warsaw', 'Amsterdam'],
    'Country': ['United Kingdom', 'France', 'Germany', 'Spain', 'Italy', 'Austria', 'Poland', 'Netherlands'],
    'Latitude': [51.5074, 48.8566, 52.5200, 40.4168, 41.9028, 48.2082, 52.2297, 52.3676],
    'Longitude': [-0.1278, 2.3522, 13.4050, -3.7038, 12.4964, 16.3738, 21.0122, 4.9041]
}
gdf = gpd.GeoDataFrame(
    data,
    geometry=gpd.points_from_xy(data['Longitude'], data['Latitude']),
    crs="EPSG:4326",
)

gdf.hvplot.points(tiles=True, color='red')

sdf = spd.GeoDataFrame(gdf)
sdf.hvplot.points(tiles=True, color='red')

@Azaya89
Copy link
Contributor Author

Azaya89 commented Nov 26, 2024

This is where is_geodataframe is a bit misleading, as far as I can see spatialpandas objects don't contain any geospatial metadata (e.g. CRS) contrary to geopandas objects.

Is having a _geometry column not considered geospatial metadata? https://github.com/holoviz/spatialpandas/blob/main/spatialpandas/geodataframe.py#L17

@maximlt
Copy link
Member

maximlt commented Nov 26, 2024

This is where is_geodataframe is a bit misleading, as far as I can see spatialpandas objects don't contain any geospatial metadata (e.g. CRS) contrary to geopandas objects.

Is having a _geometry column not considered geospatial metadata? https://github.com/holoviz/spatialpandas/blob/main/spatialpandas/geodataframe.py#L17

That certainly means it has something geo-related but not necessarily geographic, which is what we need here to perform the conversion.

Copy link
Member

@jbednar jbednar left a comment

Choose a reason for hiding this comment

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

Looks right to me!

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.

"AttributeError: 'DataFrame' object has no attribute 'crs'" for spatialpandas polygons
5 participants