Skip to content

Commit

Permalink
change project name to mapsy
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-ht committed Nov 25, 2024
1 parent b1c4648 commit e7251a8
Show file tree
Hide file tree
Showing 36 changed files with 204 additions and 199 deletions.
62 changes: 31 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Mapy Project
# Mapsy Project

## Overview
Mapy is a Python library designed easily render static maps in python. It is designed to be simple to use and easy to integrate with existing codebases. The library supports rendering background, tiled raster, filled polygon, and other layers on the map. It directly supports geometric primitives, allowing users to directly render shapely geometries.
Mapsy is a Python library designed easily render static maps in python. It is designed to be simple to use and easy to integrate with existing codebases. The library supports rendering background, tiled raster, filled polygon, and other layers on the map. It directly supports geometric primitives, allowing users to directly render shapely geometries.

Input data must be in the `EPSG:4326` - WGS84 projection.

Expand All @@ -19,11 +19,11 @@ Input data must be in the `EPSG:4326` - WGS84 projection.


## Installation
To install the Mapy library, clone the repository and install the required dependencies:
To install the Mapsy library, clone the repository and install the required dependencies:

```bash
git clone <repository-url>
cd mapy
cd mapsy
poetry install
# or
pip install .
Expand All @@ -39,17 +39,17 @@ brew install cairo

## Usage

The Mapy library is designed to be simple to use. The following sections provide examples of how to create a map with different layers. Note that in almost all cases you would have to add an attribution layer to the map. For example, if you use OpenStreetMap tiles, you would have to add the OpenStreetMap attribution to the map. This is not done automatically!
The Mapsy library is designed to be simple to use. The following sections provide examples of how to create a map with different layers. Note that in almost all cases you would have to add an attribution layer to the map. For example, if you use OpenStreetMap tiles, you would have to add the OpenStreetMap attribution to the map. This is not done automatically!


### Creating a simple Map

Here is an example of how to create a simple map with a filled polygon:

```python
import mapy
my_map = mapy.Map()
tile_layer = mapy.TiledRasterLayer(
import mapsy
my_map = mapsy.Map()
tile_layer = mapsy.TiledRasterLayer(
[
"https://tile.openstreetmap.org/{z}/{x}/{y}.png",
]
Expand All @@ -58,8 +58,8 @@ my_map.add_layer(tile_layer)
my_map.add_layer(Attribution("© OpenStreetMap contributors"))

surf = my_map.render(
mapy.FixedScreenSize(
Box.from_lng_lat(5.988, 47.302, 15.016, 54.983), mapy.ScreenSize(512, 512)
mapsy.FixedScreenSize(
Box.from_lng_lat(5.988, 47.302, 15.016, 54.983), mapsy.ScreenSize(512, 512)
)
)
surf.write_to_png("my_map.png")
Expand All @@ -74,15 +74,15 @@ If you want to use the map on a public place be sure to include proper attributi
A background layer provides a solid color background for the map.

```python
background_layer = mapy.BackgroundLayer(mapy.Color(1, 1, 1))
background_layer = mapsy.BackgroundLayer(mapsy.Color(1, 1, 1))
my_map.add_layer(background_layer)
```

#### Tiled Raster Layer
A tiled raster layer allows the use of map tiles from sources like OpenStreetMap.

```python
tile_layer = mapy.TiledRasterLayer(
tile_layer = mapsy.TiledRasterLayer(
[
"https://tile.openstreetmap.org/{z}/{x}/{y}.png",
]
Expand All @@ -97,12 +97,12 @@ A fill layer can be used to add filled polygons with customizable colors and bor
from shapely.geometry import shape

polygon = shape(json)
fill_layer = mapy.FillLayer(
fill_layer = mapsy.FillLayer(
[
mapy.FillItem(
mapsy.FillItem(
polygon,
fill_color=mapy.Color(0.5, 0.5, 0.5, 0.3),
line_color=mapy.Color(0, 0, 0),
fill_color=mapsy.Color(0.5, 0.5, 0.5, 0.3),
line_color=mapsy.Color(0, 0, 0),
line_width=2,
)
]
Expand All @@ -117,12 +117,12 @@ A line layer can be used to show LineStrings on the map
from shapely.geometry import shape

line = shape(json)
fill_layer = mapy.LineLayer(
fill_layer = mapsy.LineLayer(
[
mapy.LineItem(
mapsy.LineItem(
line,
join=mapy.LineJoin.round,
cap=mapy.LineCap.round
join=mapsy.LineJoin.round,
cap=mapsy.LineCap.round
width=12,
outline_width=3,
outline_color=Colors.BLACK,
Expand All @@ -144,12 +144,12 @@ A circle layer can be used to show Points on the map
from shapely.geometry import shape

point = shape(json)
circle_layer = mapy.CircleLayer(
circle_layer = mapsy.CircleLayer(
[
mapy.CircleItem(
mapsy.CircleItem(
point,
fill_color=mapy.Color(0.5, 0.5, 0.5, 0.3),
line_color=mapy.Color(0, 0, 0),
fill_color=mapsy.Color(0.5, 0.5, 0.5, 0.3),
line_color=mapsy.Color(0, 0, 0),
line_width=2,
radius=10,
)
Expand All @@ -159,7 +159,7 @@ my_map.add_layer(circle_layer)
```

#### Symbol Layer
A symbol layer can be used to show Points on the map. You can load custom icons by using the `mapy.Icon.from_path` class method.
A symbol layer can be used to show Points on the map. You can load custom icons by using the `mapsy.Icon.from_path` class method.

##### Limitations
- The text is not automatically placed relative to the symbol. You have to calculate the position yourself.
Expand All @@ -172,11 +172,11 @@ A symbol layer can be used to show Points on the map. You can load custom icons
from shapely.geometry import shape

point = shape(json)
symbol_layer = mapy.SymbolLayer(
symbol_layer = mapsy.SymbolLayer(
[
mapy.SymbolItem(
mapsy.SymbolItem(
point,
icon=mapy.Icons.PIN_24,
icon=mapsy.Icons.PIN_24,
text="Hello World",
text_offset=(0, 16)
)
Expand All @@ -185,7 +185,7 @@ symbol_layer = mapy.SymbolLayer(
my_map.add_layer(symbol_layer)
```

You can set the anchor of the text with the `text_anchor` parameter. The default is `mapy.TextAnchor.BOTTOM_LEFT`. The following options are available:
You can set the anchor of the text with the `text_anchor` parameter. The default is `mapsy.TextAnchor.BOTTOM_LEFT`. The following options are available:

| | | |
| - | - | - |
Expand All @@ -199,7 +199,7 @@ You can set the anchor of the text with the `text_anchor` parameter. The default
An attribution layer can be used to add attribution to the map. This is important if you use tiles from a public source like OpenStreetMap.

```python
attribution = mapy.Attribution("© OpenStreetMap contributors")
attribution = mapsy.Attribution("© OpenStreetMap contributors")
my_map.add_layer(attribution)
```

Expand All @@ -215,7 +215,7 @@ pytest

## Output Example

The image below is an example of a map created using the Mapy library:
The image below is an example of a map created using the Mapsy library:

![Enforced Bounding Box](images/EnforcedBBox.png)

Expand Down
38 changes: 19 additions & 19 deletions example/complex_map.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import json
from typing import Any

import mapy
import mapsy
from shapely.geometry import shape, Polygon

import random
from mapy.geo_util import Box, merge_bounds
from mapsy.geo_util import Box, merge_bounds


def load_geojson(file_path: str) -> tuple[list[Polygon], dict[str, Any]]:
Expand All @@ -21,59 +21,59 @@ def load_geojson(file_path: str) -> tuple[list[Polygon], dict[str, Any]]:
return geoms, properties


def build_fill_items(polygons: list[Polygon]) -> list[mapy.FillItem]:
def build_fill_items(polygons: list[Polygon]) -> list[mapsy.FillItem]:
items = []
for poly in polygons:
fill_color = mapy.Color.from_hsv(random.random(), 0.7, 0.5, 0.2)
line_color = mapy.Color(0, 0, 0, 0.6)
fill_color = mapsy.Color.from_hsv(random.random(), 0.7, 0.5, 0.2)
line_color = mapsy.Color(0, 0, 0, 0.6)
line_width = 1
items.append(mapy.FillItem(poly, fill_color, line_color, line_width))
items.append(mapsy.FillItem(poly, fill_color, line_color, line_width))
return items


def build_symbol_items(
polygons: list[Polygon], properties: list[dict[str, Any]]
) -> list[mapy.SymbolItem]:
) -> list[mapsy.SymbolItem]:
items = []
for poly, props in zip(polygons, properties):
poly.centroid
text = props["NAME_2"]
symbol_item = mapy.SymbolItem(
symbol_item = mapsy.SymbolItem(
poly.centroid,
text=text,
text_weight=mapy.FontWeight.BOLD,
text_weight=mapsy.FontWeight.BOLD,
text_size=18,
text_color=mapy.Colors.BLACK,
text_outline_color=mapy.Colors.WHITE,
text_color=mapsy.Colors.BLACK,
text_outline_color=mapsy.Colors.WHITE,
text_outline_width=2,
text_anchor=mapy.TextAnchor.CENTER,
text_anchor=mapsy.TextAnchor.CENTER,
text_offset=(0, 40) if text == "Brandenburg" else (0, 0),
)
items.append(symbol_item)
return items


def main():
map = mapy.Map()
map = mapsy.Map()
random.seed(0)
geoms, properties = load_geojson("example/districts_germany.json")
bboxes = [Box(*geom.bounds) for geom in geoms]
bbox = merge_bounds(bboxes).with_relative_padding(0.05)

tile_layer = mapy.TiledRasterLayer(
tile_layer = mapsy.TiledRasterLayer(
[
"https://tile.openstreetmap.org/{z}/{x}/{y}.png",
]
)

map.add_layer(tile_layer)
map.add_layer(mapy.FillLayer(build_fill_items(geoms)))
map.add_layer(mapy.SymbolLayer(build_symbol_items(geoms, properties)))
map.add_layer(mapy.Attribution("© OpenStreetMap contributors"))
map.add_layer(mapsy.FillLayer(build_fill_items(geoms)))
map.add_layer(mapsy.SymbolLayer(build_symbol_items(geoms, properties)))
map.add_layer(mapsy.Attribution("© OpenStreetMap contributors"))

render_mode = mapy.FixedScreenSize(bbox, mapy.ScreenSize(1400, 1175))
render_mode = mapsy.FixedScreenSize(bbox, mapsy.ScreenSize(1400, 1175))
map.render(render_mode).write_to_png("images/EnforcedScreenSize.png")
render_mode = mapy.FixedBBox(bbox, 1000**2)
render_mode = mapsy.FixedBBox(bbox, 1000**2)
map.render(render_mode).write_to_png("images/EnforcedBBox.png")


Expand Down
26 changes: 13 additions & 13 deletions example/line_types.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import mapy
import mapsy
from shapely import LineString
from shapely import affinity

from mapy.color import Color, Colors
from mapy.common import LineCap, LineJoin
from mapsy.color import Color, Colors
from mapsy.common import LineCap, LineJoin


def simple_line(y_offset: int = 0, x_offset: int = 0) -> LineString:
Expand All @@ -15,7 +15,7 @@ def simple_line(y_offset: int = 0, x_offset: int = 0) -> LineString:


def make_line_variants():
map = mapy.Map()
map = mapsy.Map()
lines = []
texts = []

Expand All @@ -26,7 +26,7 @@ def add_items(
join_to_set = join if join is not None else LineJoin.MITER
text = f"CAP {cap.value}" if cap else f"JOIN {join.value}"
lines.append(
mapy.LineItem(
mapsy.LineItem(
geometry=geometry,
color=color,
cap=cap_to_set,
Expand All @@ -37,14 +37,14 @@ def add_items(
)
)
texts.append(
mapy.SymbolItem(
mapsy.SymbolItem(
text=text,
geometry=affinity.translate(geometry.centroid, xoff=-1),
text_color=Colors.BLACK,
text_size=14,
text_font="times",
text_weight=mapy.FontWeight.BOLD,
text_slant=mapy.FontSlant.NORMAL,
text_weight=mapsy.FontWeight.BOLD,
text_slant=mapsy.FontSlant.NORMAL,
)
)

Expand All @@ -59,13 +59,13 @@ def add_items(
line = geometries[n]
add_items(line, Colors.GRAY, join=join)

bounds = mapy.bounds_for_geometries(geometries)
map.add_layer(mapy.BackgroundLayer(Colors.WHITE))
map.add_layer(mapy.LineLayer(lines))
map.add_layer(mapy.SymbolLayer(texts))
bounds = mapsy.bounds_for_geometries(geometries)
map.add_layer(mapsy.BackgroundLayer(Colors.WHITE))
map.add_layer(mapsy.LineLayer(lines))
map.add_layer(mapsy.SymbolLayer(texts))

map.render(
mapy.FixedBBox(bounds.with_relative_padding(0.12), 400 * 400)
mapsy.FixedBBox(bounds.with_relative_padding(0.12), 400 * 400)
).write_to_png("images/line_types.png")


Expand Down
22 changes: 11 additions & 11 deletions example/simple_map.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mapy
import mapsy
from shapely.geometry import shape

from mapy.geo_util import Box
from mapsy.geo_util import Box


json = {
Expand Down Expand Up @@ -49,32 +49,32 @@

def main():
# Create a map
map = mapy.Map()
tile_layer = mapy.TiledRasterLayer(
map = mapsy.Map()
tile_layer = mapsy.TiledRasterLayer(
[
"https://tile.openstreetmap.org/{z}/{x}/{y}.png",
]
)

polygon = shape(json)
fill = mapy.FillLayer(
fill = mapsy.FillLayer(
[
mapy.FillItem(
mapsy.FillItem(
polygon,
fill_color=mapy.Color(0.5, 0.5, 0.5, 0.3),
line_color=mapy.Color(0, 0, 0),
fill_color=mapsy.Color(0.5, 0.5, 0.5, 0.3),
line_color=mapsy.Color(0, 0, 0),
line_width=2,
)
]
)

map.add_layer(mapy.BackgroundLayer(mapy.Color(1, 1, 1)))
map.add_layer(mapsy.BackgroundLayer(mapsy.Color(1, 1, 1)))
map.add_layer(tile_layer)
map.add_layer(fill)

surf = map.render(
mapy.FixedScreenSize(
Box(*polygon.buffer(0.5).bounds), mapy.ScreenSize(512, 512)
mapsy.FixedScreenSize(
Box(*polygon.buffer(0.5).bounds), mapsy.ScreenSize(512, 512)
)
)
surf.write_to_png("simple_map.png")
Expand Down
Loading

0 comments on commit e7251a8

Please sign in to comment.