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 准备地理数据表
按照上述步骤创建和导入地理数据表。
登录后查看全文
热门项目推荐
Kimi-K2.5Kimi K2.5 是一款开源的原生多模态智能体模型,它在 Kimi-K2-Base 的基础上,通过对约 15 万亿混合视觉和文本 tokens 进行持续预训练构建而成。该模型将视觉与语言理解、高级智能体能力、即时模式与思考模式,以及对话式与智能体范式无缝融合。Python00- QQwen3-Coder-Next2026年2月4日,正式发布的Qwen3-Coder-Next,一款专为编码智能体和本地开发场景设计的开源语言模型。Python00
xw-cli实现国产算力大模型零门槛部署,一键跑通 Qwen、GLM-4.7、Minimax-2.1、DeepSeek-OCR 等模型Go06
PaddleOCR-VL-1.5PaddleOCR-VL-1.5 是 PaddleOCR-VL 的新一代进阶模型,在 OmniDocBench v1.5 上实现了 94.5% 的全新 state-of-the-art 准确率。 为了严格评估模型在真实物理畸变下的鲁棒性——包括扫描伪影、倾斜、扭曲、屏幕拍摄和光照变化——我们提出了 Real5-OmniDocBench 基准测试集。实验结果表明,该增强模型在新构建的基准测试集上达到了 SOTA 性能。此外,我们通过整合印章识别和文本检测识别(text spotting)任务扩展了模型的能力,同时保持 0.9B 的超紧凑 VLM 规模,具备高效率特性。Python00
KuiklyUI基于KMP技术的高性能、全平台开发框架,具备统一代码库、极致易用性和动态灵活性。 Provide a high-performance, full-platform development framework with unified codebase, ultimate ease of use, and dynamic flexibility. 注意:本仓库为Github仓库镜像,PR或Issue请移步至Github发起,感谢支持!Kotlin08
VLOOKVLOOK™ 是优雅好用的 Typora/Markdown 主题包和增强插件。 VLOOK™ is an elegant and practical THEME PACKAGE × ENHANCEMENT PLUGIN for Typora/Markdown.Less00
项目优选
收起
deepin linux kernel
C
27
11
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
538
3.76 K
暂无简介
Dart
774
192
Ascend Extension for PyTorch
Python
343
406
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.34 K
756
🍒 Cherry Studio 是一款支持多个 LLM 提供商的桌面客户端
TypeScript
1.07 K
97
React Native鸿蒙化仓库
JavaScript
303
356
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
337
180
AscendNPU-IR
C++
86
142
openJiuwen agent-studio提供零码、低码可视化开发和工作流编排,模型、知识库、插件等各资源管理能力
TSX
987
249