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.
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.
toGeoJson() method to serialize geospatial data. Use accessorMutators to hydrate from stored WKT or GeoJSON fields.FeatureCollection directly from Laravel controllers; Laravel will auto-convert to JSON if the class implements JsonSerializable (which it does).FeatureCollection to wrap query results for efficient bulk export (e.g., seeding a mapping frontend or third-party geospatial service).GeoJson::parse($json) before validating geometries (e.g., check coordinate bounds or geometry type).ST_Contains via raw SQL, then map results back to FeatureCollection.[longitude, latitude] (not lat/long). Mixing this up breaks map rendering — double-check所有 inputs.boundingBox handling in some versions — if needed, extend FeatureCollection or manually add "bbox" in the array cast.JsonSerializable::jsonSerialize() to control decimal places (default PHP float precision may truncate).Feature with a null geometry is valid per spec but may break frontend libraries — validate early in your domain logic.ToArrayInterface. If extending for your domain, use composition (e.g., wrap a Point instead of inheritance) to avoid tight coupling.dd($feature->jsonSerialize()) to inspect internal structure before serialization — especially helpful when coordinates appear inverted or malformed.How can I help you explore Laravel packages today?