Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Geojson Laravel Package

jmikola/geojson

PHP library implementing the GeoJSON specification. Provides geometry, feature, and collection objects with serialization/deserialization support and validation-friendly structures. Install via Composer and use to model and exchange GeoJSON data.

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing via Composer: composer require jmikola/geojson. Once installed, the package provides classes like Feature, FeatureCollection, GeometryCollection, Point, Polygon, etc., following the GeoJSON specification. Your first use case is likely generating or consuming GeoJSON for a map (e.g., rendering points from a database). Example:

$point = new \Jmikola\GeoJson\Point([-122.4194, 37.7749]); // [longitude, latitude]
$feature = new \Jmikola\GeoJson\Feature($point, ['name' => 'San Francisco']);
$collection = new \Jmikola\GeoJson\FeatureCollection([$feature]);
echo json_encode($collection); // Outputs valid GeoJSON

Check the src/ directory for all available geometry classes and the tests/ folder for practical examples.

Implementation Patterns

  • Eloquent Integration: Extend models with a toGeoJson() method to serialize geospatial data. Use accessorMutators to hydrate from stored WKT or GeoJSON fields.
  • API Responses: Return FeatureCollection directly from Laravel controllers; Laravel will auto-convert to JSON if the class implements JsonSerializable (which it does).
  • Batch Processing: Use FeatureCollection to wrap query results for efficient bulk export (e.g., seeding a mapping frontend or third-party geospatial service).
  • Validation: Pre-process incoming GeoJSON requests by deserializing via GeoJson::parse($json) before validating geometries (e.g., check coordinate bounds or geometry type).
  • Spatial Queries: Combine with Doctrine DBAL or native DB functions (e.g., PostGIS) — e.g., store coordinates as GeoJSON and use ST_Contains via raw SQL, then map results back to FeatureCollection.

Gotchas and Tips

  • Coordinate Order: GeoJSON strictly follows [longitude, latitude] (not lat/long). Mixing this up breaks map rendering — double-check所有 inputs.
  • No Bounding Box Support: The package lacks built-in bbox/boundingBox handling in some versions — if needed, extend FeatureCollection or manually add "bbox" in the array cast.
  • Float Precision: For high-precision GPS data, override JsonSerializable::jsonSerialize() to control decimal places (default PHP float precision may truncate).
  • Null Geometry Handling: Feature with a null geometry is valid per spec but may break frontend libraries — validate early in your domain logic.
  • Extensibility: All geometry classes implement ToArrayInterface. If extending for your domain, use composition (e.g., wrap a Point instead of inheritance) to avoid tight coupling.
  • Debugging: Use dd($feature->jsonSerialize()) to inspect internal structure before serialization — especially helpful when coordinates appear inverted or malformed.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
mrnewport/laravel-plaid
mrnewport/laravel-repo
mrnewport/laravel-docsign
mrnewport/laravel-stow
mrnewport/laravel-flow
mrnewport/laravel-priceable
mrnewport/laravel-nestedterms
fredmeng/laravel-apidoc-generator
itstructure/laravel-multilingual-tools
pdustdar/elasticsearch
markupai/php-sdk
rlacerda83/lumem-microservice-shipment
uni-method/symfony-lib
tacticmedia/scim-server-bundle
neika-ai/symfony-bundle
mattleads/agent-publishing-bundle
glavweb/framework-bundle
glavweb/extra-bundle
glavweb/api-bundle
glavweb/admin-bundle