Weave Code
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.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai