CraueGeoBundle 技术文档
2024-12-25 01:53:37作者:平淮齐Percy
1. 安装指南
1.1 获取 Bundle
通过 Composer 下载并安装 Bundle:
composer require craue/geo-bundle
1.2 启用 Bundle
如果你没有使用 Symfony Flex,需要手动注册 Bundle:
// 在 config/bundles.php 中
return [
// ...
Craue\GeoBundle\CraueGeoBundle::class => ['all' => true],
];
或者对于 Symfony 3.4:
// 在 app/AppKernel.php 中
public function registerBundles() {
$bundles = [
// ...
new Craue\GeoBundle\CraueGeoBundle(),
];
// ...
}
1.3 准备地理数据表
如果你打算使用 GEO_DISTANCE_BY_POSTAL_CODE 函数,需要先在数据库中添加一些地理数据。
1.3.1 创建表
GeoPostalCode 实体包含了地理数据的结构。你可以通过以下命令导入:
# 在 shell 中
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
或者
# 在 shell 中
php bin/console doctrine:schema:update
1.3.2 导入地理数据
这是最繁琐的步骤:存储你需要的所有邮政编码及其地理坐标。幸运的是,获取这些信息并导入数据库并不难。
访问 GeoNames 下载你需要的国家的压缩包。例如,下载 DE.zip 并解压出 DE.txt 文件,例如解压到 /tmp/DE.txt。
创建一个继承自提供的基类的 fixture 类:
// MyCompany/MyBundle/Doctrine/Fixtures/CraueGeo/MyGeonamesPostalCodeData.php
namespace MyCompany\MyBundle\Doctrine\Fixtures\CraueGeo;
use Craue\GeoBundle\Doctrine\Fixtures\GeonamesPostalCodeData;
use Doctrine\Common\Persistence\ObjectManager;
class MyGeonamesPostalCodeData extends GeonamesPostalCodeData {
public function load(ObjectManager $manager) {
$this->clearPostalCodesTable($manager);
$this->addEntries($manager, '/tmp/DE.txt');
}
}
备份你的数据库!如果出现问题,不要责怪任何人。然后导入 fixture,记得使用 --append 参数。
根据你使用的 DoctrineFixturesBundle 版本,选择以下步骤:
DoctrineFixturesBundle < 3.0
# 在 shell 中
php bin/console doctrine:fixtures:load --append --fixtures="src/MyCompany/MyBundle/Doctrine/Fixtures/CraueGeo"
DoctrineFixturesBundle >= 3.1
- 注册 fixture 为服务并指定一个组。
# 在 app/config/config.yml 中
services:
my_geonames_postal_code_data:
class: MyCompany\MyBundle\Doctrine\Fixtures\CraueGeo\MyGeonamesPostalCodeData
public: false
tags:
- { name: doctrine.fixture.orm, group: my_geo_data }
- 加载该组的 fixture。
# 在 shell 中
php bin/console doctrine:fixtures:load --append --group=my_geo_data
2. 项目使用说明
假设你有一个包含国家和邮政编码的实体 Poi。现在你希望找到所有在特定地理距离内(半径为 $radiusInKm)的实体,并按距离排序。
use MyCompany\MyBundle\Entity\Poi;
// 示例值,可能来自表单,记得先验证/清理
$country = 'DE';
$postalCode = '10115';
$radiusInKm = 10;
// 创建查询构建器
$queryBuilder = $this->getDoctrine()->getEntityManager()->getRepository(Poi::class)->createQueryBuilder('poi');
// 构建查询
$queryBuilder
->select('poi, GEO_DISTANCE_BY_POSTAL_CODE(:country, :postalCode, poi.country, poi.postalCode) AS HIDDEN distance')
->having('distance <= :radius')
->setParameter('country', $country)
->setParameter('postalCode', $postalCode)
->setParameter('radius', $radiusInKm)
->orderBy('distance')
;
3. 项目 API 使用文档
3.1 GEO_DISTANCE
该函数计算两个地理坐标之间的距离,单位为公里。
参数:
latitude1: 起点的纬度longitude1: 起点的经度latitude2: 终点的纬度longitude2: 终点的经度
返回值:
- 距离(单位:公里)
3.2 GEO_DISTANCE_BY_POSTAL_CODE
该函数通过邮政编码计算两个地点之间的距离,单位为公里。
参数:
country1: 起点国家代码postalCode1: 起点邮政编码country2: 终点国家代码postalCode2: 终点邮政编码
返回值:
- 距离(单位:公里)
4. 项目安装方式
4.1 通过 Composer 安装
composer require craue/geo-bundle
4.2 手动注册 Bundle
// 在 config/bundles.php 中
return [
// ...
Craue\GeoBundle\CraueGeoBundle::class => ['all' => true],
];
或者对于 Symfony 3.4:
// 在 app/AppKernel.php 中
public function registerBundles() {
$bundles = [
// ...
new Craue\GeoBundle\CraueGeoBundle(),
];
// ...
}
4.3 准备地理数据表
按照上述步骤创建和导入地理数据表。
登录后查看全文
热门项目推荐
GLM-5智谱 AI 正式发布 GLM-5,旨在应对复杂系统工程和长时域智能体任务。Jinja00
GLM-5.1GLM-5.1是智谱迄今最智能的旗舰模型,也是目前全球最强的开源模型。GLM-5.1大大提高了代码能力,在完成长程任务方面提升尤为显著。和此前分钟级交互的模型不同,它能够在一次任务中独立、持续工作超过8小时,期间自主规划、执行、自我进化,最终交付完整的工程级成果。Jinja00
MiniMax-M2.7MiniMax-M2.7 是我们首个深度参与自身进化过程的模型。M2.7 具备构建复杂智能体应用框架的能力,能够借助智能体团队、复杂技能以及动态工具搜索,完成高度精细的生产力任务。Python00- QQwen3.5-397B-A17BQwen3.5 实现了重大飞跃,整合了多模态学习、架构效率、强化学习规模以及全球可访问性等方面的突破性进展,旨在为开发者和企业赋予前所未有的能力与效率。Jinja00
HY-Embodied-0.5这是一套专为现实世界具身智能打造的基础模型。该系列模型采用创新的混合Transformer(Mixture-of-Transformers, MoT) 架构,通过潜在令牌实现模态特异性计算,显著提升了细粒度感知能力。Jinja00
LongCat-AudioDiT-1BLongCat-AudioDiT 是一款基于扩散模型的文本转语音(TTS)模型,代表了当前该领域的最高水平(SOTA),它直接在波形潜空间中进行操作。00
热门内容推荐
项目优选
收起
deepin linux kernel
C
28
15
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
660
4.26 K
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.54 K
894
Ascend Extension for PyTorch
Python
505
610
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
392
289
暂无简介
Dart
909
219
🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解
Java
69
21
昇腾LLM分布式训练框架
Python
142
168
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
940
867
🍒 Cherry Studio 是一款支持多个 LLM 提供商的桌面客户端
TypeScript
1.33 K
108