Skip to content

Commit

Permalink
Merge pull request #22 from makinacorpus/add_catalogs_by_folder
Browse files Browse the repository at this point in the history
Add catalogs by folder
  • Loading branch information
leplatrem committed Sep 11, 2013
2 parents 0e9a4c6 + 5c743f7 commit c1175fe
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 77 deletions.
22 changes: 6 additions & 16 deletions example/livembtiles/urls.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
from django.conf.urls.defaults import patterns, include, url
from django.views.generic import ListView, TemplateView
from django.conf.urls import patterns, include, url

from mbtilesmap.models import MBTiles
from mbtilesmap import MBTILES_ID_PATTERN
from mbtilesmap import MBTILES_CATALOG_PATTERN, MBTILES_ID_PATTERN
from .views import index, catalog, preview


class MyTemplateView(TemplateView):
def get_context_data(self, **kwargs):
return self.kwargs

urlpatterns = patterns('',
url(r'^$',
ListView.as_view(queryset=MBTiles.objects.all(),
context_object_name='maps',
template_name='index.html'),
name="index"),
url(r'^(?P<name>%s)/$' % MBTILES_ID_PATTERN,
MyTemplateView.as_view(template_name='map.html'),
name="map"),
url(r'^$', index, name="index"),
url(r'^(?P<catalog>%s)/$' % MBTILES_CATALOG_PATTERN, catalog, name="catalog"),
url(r'^(?P<catalog>%s)/(?P<name>%s)/$' % (MBTILES_CATALOG_PATTERN, MBTILES_ID_PATTERN), preview, name="preview"),
url(r'^', include('mbtilesmap.urls', namespace='mb', app_name='mbtilesmap')),
)
2 changes: 2 additions & 0 deletions mbtilesmap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@


MBTILES_ID_PATTERN = r'[\.\-_0-9a-zA-Z]+'
MBTILES_CATALOG_PATTERN = MBTILES_ID_PATTERN


app_settings = EasyDict(dict(
MBTILES_EXT = 'mbtiles',
Expand Down
63 changes: 44 additions & 19 deletions mbtilesmap/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@ def __init__(self, *args, **kwargs):
class MBTilesManager(object):
""" List available MBTiles in MBTILES_ROOT """
def __init__(self, *args, **kwargs):
if not os.path.exists(app_settings.MBTILES_ROOT):
raise MBTilesFolderError()
self.folder = app_settings.MBTILES_ROOT
for path in self._paths:
if not os.path.exists(path):
raise MBTilesFolderError()
self.folder = self._paths[0]

def filter(self, catalog=None):
if catalog:
self.folder = self.catalog_path(catalog)
return self

def all(self):
return self
Expand All @@ -50,28 +56,47 @@ def __iter__(self):
except (AssertionError, InvalidFormatError), e:
logger.error(e)

@property
def _paths(self):
return app_settings.MBTILES_ROOT.split(':')

def default_catalog(self):
path = self._paths[0]
return os.path.basename(path)

def catalog_path(self, catalog):
try:
return [p for p in self._paths if p.endswith(catalog)][0]
except IndexError:
raise MBTilesNotFoundError(_("Catalog '%s' not found.") % catalog)

def fullpath(self, name, catalog=None):
if os.path.exists(name):
return name

if catalog is None:
basepath = self.folder
else:
basepath = self.catalog_path(catalog)

mbtiles_file = os.path.join(basepath, name)
if os.path.exists(mbtiles_file):
return mbtiles_file

mbtiles_file = "%s.%s" % (mbtiles_file, app_settings.MBTILES_EXT)
if os.path.exists(mbtiles_file):
return mbtiles_file

raise MBTilesNotFoundError(_("'%s' not found in %s") % (mbtiles_file, self._paths))


class MBTiles(object):
""" Represent a MBTiles file """

objects = MBTilesManager()

def __init__(self, name):
"""
Load a MBTile file.
If `name` is a valid filepath, it will load it.
Else, it will attempt to load it within `settings.MBTILES_ROOT` folder.
"""
mbtiles_file = name
if not os.path.exists(mbtiles_file):
if not os.path.exists(app_settings.MBTILES_ROOT):
raise MBTilesFolderError()
mbtiles_file = os.path.join(app_settings.MBTILES_ROOT, name)
if not os.path.exists(mbtiles_file):
mbtiles_file = "%s.%s" % (mbtiles_file, app_settings.MBTILES_EXT)
if not os.path.exists(mbtiles_file):
raise MBTilesNotFoundError(_("'%s' not found") % mbtiles_file)
self.fullpath = mbtiles_file
def __init__(self, name, catalog=None):
self.fullpath = self.objects.fullpath(name, catalog)
self.basename = os.path.basename(self.fullpath)
self._reader = MBTilesReader(self.fullpath, tilesize=app_settings.TILE_SIZE)

Expand Down
Loading

0 comments on commit c1175fe

Please sign in to comment.