andreshg112 / datos-abiertos
在平台https://www.datos.gov.co上查询哥伦比亚政府开放数据API
Requires
- php: ^7.1
- allejo/php-soda: ^1.0
- illuminate/support: ^5.5
- spatie/macroable: ^1.0
Requires (Dev)
- codedungeon/phpunit-result-printer: ^0.26.1
- orchestra/testbench: 3.8.*
- phpunit/phpunit: ^7.0
This package is auto-updated.
Last update: 2024-09-26 12:35:47 UTC
README
此包封装了对哥伦比亚政府开放数据API的查询。
实现资源
-
学前教育、基础教育机构 (
\Andreshg112\DatosAbiertos\Facades\Colegios
). -
国家政治行政区域代码 (
\Andreshg112\DatosAbiertos\Facades\Divipola
). -
交通机构 (
\Andreshg112\DatosAbiertos\Facades\OrganismosTransito
).
如果你希望开发对其他datos.gov.co数据资源的查询,可以在问题中提出请求。
要求
- Laravel >= 5.5
- PHP >= 7.1.x
如果你需要支持低于指定版本的版本,请将请求留在我问题中,我会看看我能做什么。
安装
您可以通过composer安装此包
composer require andreshg112/datos-abiertos
此包使用Laravel Package Discovery,因此,如果您使用Laravel 5.5或更高版本,您不需要在config/app.php
文件中的providers
列表中添加\Andreshg112\DatosAbiertos\DatosAbiertosServiceProvider
。
使用
基本使用
每个资源都继承自\Andreshg112\DatosAbiertos\Datasets\BaseDataset
,它有一个getData($filterOrSoqlQuery = '')
方法,允许查询资源中的所有记录或根据参数进行过滤。
例如
use Andreshg112\DatosAbiertos\Facades\Divipola; // Listado total de municipios: $data = Divipola::getData();
使用过滤器
在Divipola API的定义中可以找到资源的详细信息和使用过滤器的说明。
警告:数据.gov.co API区分大小写。因此,如果你搜索例如
nom_mpio=valledupar
,它不会出现,因为必须有大写的V
。
use Andreshg112\DatosAbiertos\Facades\Divipola; // Para filtrar por código de departamento puedes hacer cualquiera de las siguientes formas: $filterOrSoqlQuery = 'cod_depto=20'; // esta $filterOrSoqlQuery = ['cod_depto' => '20']; // o esta $data = Divipola::getData($filterOrSoqlQuery);
此包使用allejo/php-soda通过Socrata Open Data API (SODA)对数据.gov.co的资源进行请求,因此
$filterOrSoqlQuery
可以是函数\allejo\Socrata\SodaDataset::getData(filterOrSoqlQuery)
接受的任何参数。要使用高级过滤器,请参阅其文档(英文)。
扩展功能
宏可扩展
资源(数据集)的父类\Andreshg112\DatosAbiertos\Datasets\BaseDataset
使用spatie/macroable的trait,因此您可以按以下方式扩展每个资源的功能
// En el archivo app/Providers/AppServiceProvider.php: use Andreshg112\DatosAbiertos\Facades\OrganismosTransito; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { OrganismosTransito::macro('whereEstadoLimit', function($estado, $limit){ $filter = [ '$where' => "estado = '$estado'", // https://dev.socrata.com/docs/queries '$limit' => $limit, // 1, 2, 3, etc. ]; return $this->getData($filter); }); } } // Y luego, en cualquier otra parte: $data = OrganismosTransito::whereEstadoLimit('ACTIVO', 3);
更多信息请参阅其文档。
创建你自己的资源
您可以通过以下方式创建一个继承自 \Andreshg112\DatosAbiertos\Datasets\BaseDataset
的自定义数据集:
此资源将在包中实现,此处仅作为示例。
<?php use Andreshg112\DatosAbiertos\Datasets\BaseDataset; namespace App\Datasets; /** * https://www.datos.gov.co/Educaci-n/colegios/u3ch-n6ec */ class Colegios extends BaseDataset { public function getColumns() { return [ 'a_o', // año. 'calendario', 'codigo_etc', // nombre_Rector 'codigodepartamento', 'codigoestablecimiento', 'codigomunicipio', 'correo_electronico', 'direccion', 'discapacidades', 'especialidad', 'estrato_socio_economico', 'etnias', 'grados', 'idiomas', 'internado', 'jornada', 'matricula_contratada', 'modelos', 'modelos_educativos', 'niveles', 'nombredepartamento', 'nombreestablecimiento', 'nombremunicipio', 'numero_de_sedes', 'prestador_de_servicio', 'propiedad_planta_fisica', 'resguardo', 'secretaria', 'telefono', 'tipo_establecimiento', 'zona', ]; } protected function getDatasetIdentifier() { // El código del recurso, que es la última parte de la URL sin el .json // https://www.datos.gov.co/resource/xax6-k7eu.json return 'xax6-k7eu'; } protected function getUniqueColumn() { return 'codigoestablecimiento'; } }
API 不接受带有重音符号的字符、
ñ
或特殊字符。
然后,您需要为该类创建一个 门面,这样就可以使用了。可以是 实时门面,通过在类命名空间前放置命名空间 Facades
来实现,如下所示:
use Facades\App\Datasets\Colegios;
资源(数据集)的方法
所有资源都允许通过 getData($filterOrSoqlQuery)
方法进行高级筛选,但对于简单的搜索,可以使用以下方式使用 where(string $column, $value)
方法:
use Facades\App\Datasets\Colegios; // Fachada en tiempo real. $data = Colegios::where('modelos_educativos', 'EDUCACIÓN TRADICIONAL');
资源或数据集(包括或自定义)的列,在 getColumns()
中指定,允许根据 camelCase 名称动态访问方法,并且使用以大写字母开头的 where
前缀,例如:
use Facades\App\Datasets\Colegios; use Andreshg112\DatosAbiertos\Facades\Divipola; use Andreshg112\DatosAbiertos\Facades\OrganismosTransito; // nombredepartamento Colegios::whereNombredepartamento('Cesar'); // d minúscula porque no tiene subguión. // nom_mpio Divipola::whereNomMpio('Valledupar'); // M mayúscula porque tiene subguión. // categor_a OrganismosTransito::whereCategorA('A'); // A mayúscula porque tiene un subguión antes.
所有方法都返回一个记录数组(数组中的数组)。如果想要通过具有唯一值的列查询单个记录,则可以使用 find($uniqueValue)
函数。这会使该方法只返回一个记录(数组)而不是记录数组。例如:
use Andreshg112\DatosAbiertos\Facades\Colegios; // Esta es la implementación incluída. $data = Colegios::find(147707000156); // Es equivalente a: $data = Colegios::whereCodigoestablecimiento(147707000156)[0] ?? null; // y a: $data = Colegios::where('codigoestablecimiento', 147707000156)[0] ?? null;
唯一值列
- 学校:codigo_establecimiento
- Divipola:cod_mpio
- OrganismosTransito:cod_divipola
列名在 Datos Abiertos 平台的每个资源 API 定义中指定。
测试
composer test
变更日志
请查看 版本列表 以获取有关最近更改的更多信息。
贡献
请查看 CONTRIBUTING 文件(英文)以获取更多详细信息。
安全性
如果您发现与安全相关的漏洞或错误,请通过发送电子邮件至 andreshg112@gmail.com 而不是通过错误报告记录来告知我。
致谢
许可证
MIT 许可证。请查看 许可证文件(英文)以获取更多信息。
Laravel 包模板
此包是使用 Laravel 包模板 生成的。