rmwiesenberg / gview

In-browser viewer of 2D and 3D geographic data.
https://gview.rwies.com/
BSD 3-Clause "New" or "Revised" License
1 stars 1 forks source link

Automatically parse style properties if they exist. #38

Open mattdreisbach opened 1 month ago

mattdreisbach commented 1 month ago

There is no way to specify style within the geopapckage.

Can you parse the geopackage for style properties if they exist?

Perhaps you could use standard QGIS Styling names:

 "fill": "#ff0000",
 "fill-opacity": 0.5,
 "radius": 1.5,
 "stroke": "#000000",
 "stroke-width": 1,
 "stroke-opacity": 1.0

Example:


import geopandas as gpd
from shapely.geometry import Point, Polygon

# Define the coordinates for three points and one polygon
points = [
    {"geometry": Point(-85.50, 42.98), "name": "Detroit", "fill": "#ff0000", "radius": 100000},
    {"geometry": Point(-84.68, 42.70), "name": "Lansing", "fill": "#00ff00", "radius": 100000},
    {"geometry": Point(-87.98, 41.86), "name": "Chicago", "fill": "#0000ff", "radius": 100000}
]

polygon_coords = [
    (-84., 45.),
    (-87., 41.),
    (-81., 41.),
    (-84., 45.)
]

polygon = [{"geometry": Polygon(polygon_coords), "name": "Michigan", "fill": "#808080"}]

# Create GeoDataFrames
points_gdf = gpd.GeoDataFrame(points, crs="EPSG:4326")
polygon_gdf = gpd.GeoDataFrame(polygon, crs="EPSG:4326")

# Save to a GeoPackage
points_gdf.to_file("michigan_data.gpkg", layer='points', driver="GPKG")
polygon_gdf.to_file("michigan_data.gpkg", layer='polygon', driver="GPKG", mode='a')

Should yield something like this (colors incorrect for points) image

gejson could also support this:

{
 "type": "FeatureCollection",
 "features": [
   {
     "type": "Feature",
     "geometry": {
       "type": "Polygon",
       "coordinates": [
         [
           [102.0, 0.0],
           [103.0, 1.0],
           [104.0, 0.0],
           [102.0, 0.0]
         ]
       ]
     },
     "properties": {
       "name": "Sample Polygon",
       "style": {
         "fill": "#ff0000",
         "fill-opacity": 0.5,
         "stroke": "#000000",
         "stroke-width": 1,
         "stroke-opacity": 1.0
       }
     }
   }
 ]
}

should yield something like this: image

mattdreisbach commented 1 month ago

After working with QGIS for some more time it seems that the alpha channel can be included in the color for example:

 "fill": "#ff000000",  // patern is #aarrggbb so this is black with no opacity
 // "fill-opacity": 0.5,  // no longer needed
 "radius": 1.5,
 "stroke": "#800000ff",  // patern is #aarrggbb so this is blue with 50% opacity
 "stroke-width": 1,
 // "stroke-opacity": 1.0// no longer needed
rmwiesenberg commented 1 month ago

Partially done. I think we match QGIS now for color. The stroke/radius piece is a little more work as they aren't tied to a field yet.

Also, regardless of file type (gpkg, geojson, kml) if there is a field called fill or stroke they are also pulled by default to be the fill or stroke items.

mattdreisbach commented 1 month ago

Works like a champ! Playing around with this, seems you encoded the alpha bytes at the end: #rrggbbaa but the standard encoding puts the alpha bytes first: #aarrggbb

rmwiesenberg commented 1 month ago

Oh weird. I'm following the css standard https://developer.mozilla.org/en-US/docs/Web/CSS/hex-color

QGIS has "admitted" that the #aarrggbb is a mistake that they won't fix 😂

rmwiesenberg commented 1 month ago

(the three and four color hex codes won't work right now either, but imma PR that in a minute)

mattdreisbach commented 1 month ago

:fire: