适用于户外和室内地图的django-app可重复使用组件
项目描述
用于存储对象GIS和室内坐标的可重复使用django-app。
依赖
Python >= 3.8
GeoDjango (查看GeoDjango安装说明)
GeoDjango支持的一个数据库
兼容性表
django-loci |
Python版本 |
0.2 |
2.7或>=3.4 |
0.3 - 0.4 |
>=3.6 |
1.0 |
>=3.7 |
dev |
>=3.8 |
从PyPI安装稳定版本
从PyPI安装
pip install django-loci
安装开发版本
首先,安装GeoDjango的依赖项
安装tar包
pip install https://github.com/openwisp/django-loci/tarball/master
或者您也可以通过pip使用git安装。
pip install -e git+git://github.com/openwisp/django-loci#egg=django_loci
如果您想贡献,请安装您克隆的分支。
git clone git@github.com:<your_fork>/django-loci.git
cd django_loci
python setup.py develop
设置(集成到现有django项目中)
首先,将您的数据库引擎设置为GeoDjango支持的空间数据库之一。
按照以下顺序将django_loci及其依赖项添加到INSTALLED_APPS中
INSTALLED_APPS = [
# ...
"django.contrib.gis",
"django_loci",
"django.contrib.admin",
"leaflet",
"channels"
# ...
]
根据您的需求配置CHANNEL_LAYERS,以下是一个示例配置
ASGI_APPLICATION = "django_loci.channels.asgi.channel_routing"
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels.layers.InMemoryChannelLayer",
},
}
现在运行迁移
./manage.py migrate
故障排除
安装GeoDjango时常见的问题和解决方案。
无法加载SpatiaLite库扩展
如果您遇到以下异常
django.core.exceptions.ImproperlyConfigured: Unable to load the SpatiaLite library extension
您需要在settings.py中指定SPATIALITE_LIBRARY_PATH
,如django关于如何安装和配置spatialte的文档中所述。
与其他地理空间库的问题
设置
LOCI_FLOORPLAN_STORAGE
类型: |
字符串 |
默认值: |
django_loci.storage.OverwriteStorage |
用于上传平面图图像的django文件存储类。
只要文件存储有upload_to
类方法,它就会被传递给FloorPlan.image.upload_to
,您就可以将其更改为不同的存储。
要了解此语句的详细信息,请查看django_loci.storage.OverwriteStorage的代码。
DJANGO_LOCI_GEOCODER
类型: |
字符串 |
默认值: |
ArcGIS |
用于地理编码和反向地理编码的服务。
支持的地理位置服务
ArcGIS
Nominatim
GoogleV3
(Google Maps v3)
DJANGO_LOCI_GEOCODE_FAILURE_DELAY
类型: |
整数 |
默认值: |
1 |
地理编码请求失败时,在重试API调用之间等待的秒数。
DJANGO_LOCI_GEOCODE_RETRIES
类型: |
整数 |
默认值: |
3 |
地理编码请求失败时的重试API调用次数。
DJANGO_LOCI_GEOCODE_API_KEY
类型: |
字符串 |
默认值: |
None |
如果需要,请提供API密钥(例如:Google Maps)。
系统检查
地理编码
用于检查地理编码是否按预期工作。
使用以下命令运行此检查
./manage.py check --deploy --tag geocoding
扩展django-loci
django-loci提供了一组模型和admin类,这些模型和admin类可以被第三方应用导入、扩展和重用。
要扩展django-loci,您绝对不应该将其添加到settings.INSTALLED_APPS
中,但您必须创建自己的应用(该应用将添加到settings.INSTALLED_APPS
),导入django-loci的基类,并添加您的自定义修改。
扩展模型
以下示例展示了如何通过添加对名为Organization
的另一个django模型的引用来扩展django-loci的基模型。
# models.py of your app
from django.db import models
from django_loci.base.models import (
AbstractFloorPlan,
AbstractLocation,
AbstractObjectLocation,
)
# the model ``organizations.Organization`` is omitted for brevity
# if you are curious to see a real implementation, check out django-organizations
class OrganizationMixin(models.Model):
organization = models.ForeignKey("organizations.Organization")
class Meta:
abstract = True
class Location(OrganizationMixin, AbstractLocation):
class Meta(AbstractLocation.Meta):
abstract = False
def clean(self):
# your own validation logic here...
pass
class FloorPlan(OrganizationMixin, AbstractFloorPlan):
location = models.ForeignKey(Location)
class Meta(AbstractFloorPlan.Meta):
abstract = False
def clean(self):
# your own validation logic here...
pass
class ObjectLocation(OrganizationMixin, AbstractObjectLocation):
location = models.ForeignKey(
Location, models.PROTECT, blank=True, null=True
)
floorplan = models.ForeignKey(
FloorPlan, models.PROTECT, blank=True, null=True
)
class Meta(AbstractObjectLocation.Meta):
abstract = False
def clean(self):
# your own validation logic here...
pass
扩展管理界面
在先前的Organization
示例中,您可以通过导入基admin类并使用它们注册模型来避免重复admin代码。
但首先,您需要在您的settings.py
中更改一些设置,这些设置对于在settings.INSTALLED_APPS
中未列出的情况下加载django-loci的admin模板和静态文件是必需的。
将django.forms
添加到INSTALLED_APPS
,现在它应该看起来像以下这样
INSTALLED_APPS = [
# ...
"django.contrib.gis",
"django_loci",
"django.contrib.admin",
# ↓
"django.forms", # <-- add this
# ↑
"leaflet",
"channels"
# ...
]
现在在INSTALLED_APPS
之后添加EXTENDED_APPS
INSTALLED_APPS = [
# ...
]
EXTENDED_APPS = ("django_loci",)
将openwisp_utils.staticfiles.DependencyFinder
添加到STATICFILES_FINDERS
STATICFILES_FINDERS = [
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
"openwisp_utils.staticfiles.DependencyFinder",
]
将openwisp_utils.loaders.DependencyLoader
添加到TEMPLATES
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"OPTIONS": {
"loaders": [
"django.template.loaders.filesystem.Loader",
"django.template.loaders.app_directories.Loader",
# add the following line
"openwisp_utils.loaders.DependencyLoader",
],
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
}
]
最后一步,添加FORM_RENDERER
FORM_RENDERER = "django.forms.renderers.TemplatesSetting"
然后您可以根据以下示例创建您的admin.py
文件
# admin.py of your app
from django.contrib import admin
from django_loci.base.admin import (
AbstractFloorPlanAdmin,
AbstractFloorPlanForm,
AbstractFloorPlanInline,
AbstractLocationAdmin,
AbstractLocationForm,
AbstractObjectLocationForm,
AbstractObjectLocationInline,
)
from django_loci.models import FloorPlan, Location, ObjectLocation
class FloorPlanForm(AbstractFloorPlanForm):
class Meta(AbstractFloorPlanForm.Meta):
model = FloorPlan
class FloorPlanAdmin(AbstractFloorPlanAdmin):
form = FloorPlanForm
class LocationForm(AbstractLocationForm):
class Meta(AbstractLocationForm.Meta):
model = Location
class FloorPlanInline(AbstractFloorPlanInline):
form = FloorPlanForm
model = FloorPlan
class LocationAdmin(AbstractLocationAdmin):
form = LocationForm
inlines = [FloorPlanInline]
class ObjectLocationForm(AbstractObjectLocationForm):
class Meta(AbstractObjectLocationForm.Meta):
model = ObjectLocation
class ObjectLocationInline(AbstractObjectLocationInline):
model = ObjectLocation
form = ObjectLocationForm
admin.site.register(FloorPlan, FloorPlanAdmin)
admin.site.register(Location, LocationAdmin)
扩展通道消费者
以这种方式扩展django-loci的channel consumer
from django_loci.channels.base import BaseLocationBroadcast
from ..models import Location # your own location model
class LocationBroadcast(BaseLocationBroadcast):
model = Location
扩展AppConfig
您可能还想重用django-loci的AppConfig
类
from django_loci.apps import LociConfig
class MyConfig(LociConfig):
name = "myapp"
verbose_name = _("My custom app")
def __setmodels__(self):
from .models import Location
self.location_model = Location
开发版安装
安装sqlite
sudo apt-get install sqlite3 libsqlite3-dev libsqlite3-mod-spatialite gdal-bin
安装您分叉的repo
git clone git://github.com/<your_fork>/django-loci
cd django-loci/
python setup.py develop
安装测试需求
pip install -r requirements-test.txt
创建数据库
cd tests/
./manage.py migrate
./manage.py createsuperuser
启动开发服务器和SMTP调试服务器
./manage.py runserver
您可以在http://127.0.0.1:8000/admin/访问管理界面。
使用以下命令运行测试
# pytests is used to test django-channels
./runtests.py && pytest
贡献
在OpenWISP 邮件列表中宣布您的意图
将该仓库进行分支并安装
编写代码
为您的代码编写测试
确保所有测试通过
确保测试覆盖率不降低
记录您的更改
发送拉取请求
变更日志
查看变更日志。
许可
查看许可证。
项目详情
下载文件
下载适用于您平台文件。如果您不确定选择哪个,请了解更多关于安装软件包的信息。