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 准备地理数据表
按照上述步骤创建和导入地理数据表。
登录后查看全文
热门项目推荐
atomcodeClaude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed. Get StartedRust0215
cann-learning-hubCANN 学习中心仓,支持在线互动运行、边学边练,提供教程、示例与优化方案,一站式助力昇腾开发者快速上手。Jupyter Notebook0138
uni-appA cross-platform framework using Vue.jsJavaScript08
GLM-5.2智谱开源 GLM-5.2,这是针对长文本任务的最新旗舰模型。相较于前代产品 GLM-5.1,它在长文本任务处理能力上实现了显著飞跃,并且首次在稳定的 100 万 token 上下文中提供这一能力。Jinja00
SwanLab⚡️SwanLab - an open-source, modern-design AI training tracking and visualization tool. Supports Cloud / Self-hosted use. Integrated with PyTorch / Transformers / LLaMA Factory / veRL/ Swift / Ultralytics / MMEngine / Keras etc.Python00
tiny-universe《大模型白盒子构建指南》:一个全手搓的Tiny-UniverseJupyter Notebook03
项目优选
收起
deepin linux kernel
C
32
16
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
471
465
暂无描述
Dockerfile
780
5.08 K
本项目是CANN提供的transformer类大模型算子库,实现网络在NPU上加速计算。
C++
878
2.03 K
Ascend Extension for PyTorch
Python
758
968
本项目是CANN提供的神经网络类计算算子库,实现网络在NPU上加速计算。
C++
698
1.4 K
昇腾LLM分布式训练框架
Python
185
231
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
1.1 K
1.14 K
本仓库是 Flutter SDK 与 Flutter Engine 的 OpenHarmony 适配版本,由 CPF-Flutter 团队维护。开发者可使用熟悉的 Flutter 技术栈开发 OpenHarmony 应用,3.35.7 及以后的适配版本可基于本仓库源码构建支持 OpenHarmony 的 Flutter Engine。
Dart
1.04 K
271
JiuwenSwarm 是一款基于openJiuwen开发的智能AI Agent,它能够将大语言模型的强大能力,通过你日常使用的各类通讯应用,直接延伸至你的指尖。
Python
2.25 K
677