Update pictograms import script to deal with category and SVG
This commit is contained in:
parent
8c774fb7b3
commit
f61f1415ce
2 changed files with 62 additions and 52 deletions
|
@ -10,11 +10,17 @@ Icons (aka pictograms in uMap sources) can be used in your map markers.
|
|||
|
||||
Icons are not embedded in uMap sources, you will have to add them manually. So you can choose which icons you want to use.
|
||||
|
||||
You can use either PNG, JPG or SVG files. SVG files are recommanded.
|
||||
|
||||
When using SVG, it's recommanded to use icons without color. UMap will switch to white colors
|
||||
automatically according to the marker background color.
|
||||
|
||||
Example of icons libraries you may want to use:
|
||||
|
||||
- [Maki Icons](https://labs.mapbox.com/maki-icons/) (icon set made for map designers)
|
||||
- [Osmic Icons](https://gitlab.com/gmgeo/osmic)
|
||||
- [SJJB Icons](http://www.sjjb.co.uk/mapicons/contactsheet)
|
||||
- [Remix](https://remixicon.com/)
|
||||
|
||||
### Import icons manually
|
||||
|
||||
|
@ -22,32 +28,15 @@ You can import icons manually by going to your uMap admin page: `https://your.se
|
|||
|
||||
### Import icons automatically
|
||||
|
||||
To import icons on your uMap server, you will need to use command `umap import_pictograms`
|
||||
To import icons on your uMap server, you will need to use command `umap import_pictograms`.
|
||||
|
||||
Note, you can get help with `umap import_pictograms -h`
|
||||
Note: you can get help with `umap import_pictograms -h`
|
||||
|
||||
In this example, we will import Maki icons.
|
||||
Basic usage:
|
||||
|
||||
First, we download icons from main site. Inside the downloaded archive, we keep only the icons folder that contains svg files. Place this folder on your server.
|
||||
umap import_pictograms --attribution "Maki Icons by Mapbox" path/to/icons/directory/
|
||||
|
||||
Go inside icons folder and remove tiny icons: `rm *-11.svg`
|
||||
### Categories
|
||||
|
||||
Now, we will use imagemagick to convert svg to png.
|
||||
|
||||
`for file in *.svg; do convert -background none $file ${file%15.svg}24.png; done`
|
||||
|
||||
To have white icons use:
|
||||
`for file in *.svg; do convert -background none -fuzz 100% -fill white -opaque black $file ${file%15.svg}24.png; done`
|
||||
|
||||
|
||||
Notes:
|
||||
- you may also want to resize image with option `-resize 24x`
|
||||
- this solution is not optimal, generated png are blurry.
|
||||
|
||||
This will convert the svg to png and rename them from `*-15.svg` to `*-24.png`
|
||||
|
||||
Now we will import icons. Note: icons names must end with `-24.png`
|
||||
|
||||
`umap import_pictograms --attribution "Maki Icons by Mapbox" icons`
|
||||
|
||||
Done. Icons are imported.
|
||||
uMap can render icons grouped into categories. When using the import script, any
|
||||
subfolder will be used as category.
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from django.core.files import File
|
||||
from django.core.management.base import BaseCommand
|
||||
|
@ -7,40 +7,61 @@ from umap.models import Pictogram
|
|||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Import pictograms from a folder'
|
||||
help = "Import pictograms from a folder"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('path')
|
||||
parser.add_argument('--attribution', required=True,
|
||||
help='Attribution of the imported pictograms')
|
||||
parser.add_argument('--suffix',
|
||||
help='Optional suffix to add to each name')
|
||||
parser.add_argument('--force', action='store_true',
|
||||
help='Update picto if it already exists.')
|
||||
parser.add_argument("path")
|
||||
parser.add_argument(
|
||||
"--attribution",
|
||||
required=True,
|
||||
help="Attribution of the imported pictograms",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--extensions",
|
||||
help="Optional list of extensins to process",
|
||||
nargs="+",
|
||||
default=[".svg"],
|
||||
)
|
||||
parser.add_argument(
|
||||
"--exclude",
|
||||
help="Optional list of files or dirs to exclude",
|
||||
nargs="+",
|
||||
default=["font"],
|
||||
)
|
||||
parser.add_argument(
|
||||
"--force", action="store_true", help="Update picto if it already exists."
|
||||
)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
path = options['path']
|
||||
attribution = options['attribution']
|
||||
suffix = options['suffix']
|
||||
force = options['force']
|
||||
for filename in os.listdir(path):
|
||||
if filename.endswith("-24.png"):
|
||||
name = self.extract_name(filename)
|
||||
if suffix:
|
||||
name = '{name}{suffix}'.format(name=name, suffix=suffix)
|
||||
self.path = Path(options["path"])
|
||||
self.attribution = options["attribution"]
|
||||
self.extensions = options["extensions"]
|
||||
self.force = options["force"]
|
||||
self.exclude = options["exclude"]
|
||||
self.handle_directory(self.path)
|
||||
|
||||
def handle_directory(self, path):
|
||||
for filename in path.iterdir():
|
||||
if filename.name in self.exclude:
|
||||
continue
|
||||
if filename.is_dir():
|
||||
self.handle_directory(filename)
|
||||
continue
|
||||
if filename.suffix in self.extensions:
|
||||
name = filename.stem
|
||||
picto = Pictogram.objects.filter(name=name).last()
|
||||
if picto:
|
||||
if not force:
|
||||
self.stdout.write(u"⚠ Pictogram with name '{name}' already exists. Skipping.".format(name=name)) # noqa
|
||||
if not self.force:
|
||||
self.stdout.write(
|
||||
f"⚠ Pictogram with name '{name}' already exists. Skipping."
|
||||
)
|
||||
continue
|
||||
else:
|
||||
picto = Pictogram()
|
||||
picto.name = name
|
||||
filepath = os.path.join(path, filename)
|
||||
with open(filepath, 'rb') as f:
|
||||
picto.attribution = attribution
|
||||
picto.pictogram.save(filename, File(f), save=True)
|
||||
self.stdout.write(u"✔ Imported pictogram {filename}.".format(filename=filename)) # noqa
|
||||
|
||||
def extract_name(self, filename):
|
||||
return filename[:-7].replace('-', ' ')
|
||||
if (path.name != self.path.name): # Subfolders only
|
||||
picto.category = path.name
|
||||
picto.attribution = self.attribution
|
||||
with (path / filename).open("rb") as f:
|
||||
picto.pictogram.save(filename.name, File(f), save=True)
|
||||
self.stdout.write(f"✔ Imported pictogram {filename}.")
|
||||
|
|
Loading…
Reference in a new issue