hular369 / geo-shapify
A PHP library that allows to check if a coordinate(lat,long) is inside or outside of a country or a circle or a polygon, calculate area occupied by a polygon/circle, and find out the nearest vertex of a country or a polygon from a given point.
Installs: 11
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/hular369/geo-shapify
Requires
- php: >=7.0
README
This package enables you to check if a coordinate is inside a defined boundary of a country or a polygon. It also provides a feature to calculate the area of a given boundary defined by a country or a polygon coordinates (latitude, longitude), and also for circles.
Features
-
Country Boundaries:
- Check if a coordinate is inside a country.
- Determine which boundary coordinate is nearest to a given point(lat, long),
- Calculate the area of a country.
-
Polygon Boundaries:
- Check if a coordinate is inside a polygon.
- Calculate the area of a polygon.
-
Circular Boundaries:
- Check if a coordinate is inside a circle defined by a center and radius.
- Calculate the area of a circle.
Usage
For Countries and Polygons:
- Define the polygon coordinates.
- Create a polygon shape using
ShapeFactory. - Check if a coordinate is inside the polygon using
contains. - Calculate the area of the polygon using
area.
For Circles:
- Create a circle shape using
ShapeFactorywith center coordinates and radius. - Check if a coordinate is inside the circle using
contains. - Calculate the area of the circle using
area.
This package enables you to check if a coordinate is inside a defined boundary of a polygon. It also facilitates with a feature to calculate area of a given boundary defined by polygon coordinates (Lat,long).
To work features on countries, you can implement as follows:
Note: currently the feature is available only for 'norway', 'sweden', 'finland', 'denmark', 'germany', 'lithuania', 'nepal'. <?php // Create a polygon shape $drawCountry = ShapeFactory::create('sweden'); // Check if a point is inside the polygon $isInside = $drawCountry->contains(56.701853, 12.319418); // Calculate the area of the polygon $area = $drawCountry->area(); // get nearest vertex of the polygon $nearestVertex = $drawCountry->nearestVertex(57.322011, 11.229478)
To create and work with polygons, you can use the ShapeFactory class as follows:
<?php // Define the coordinates of the polygon vertices $egpt = [ [31.597741, 25.112545], [22.152357, 24.958816], [22.081148, 36.719048], [31.269823, 31.338550] ]; // Create a polygon shape $drawShape = ShapeFactory::create('polygon', $egpt); // Check if a point is inside the polygon $isInside = $drawShape->contains(27.701853, 85.319418); // Calculate the area of the polygon $area = $drawShape->area(); // get nearest vertex of the polygon $nearestVertex = $drawShape->nearestVertex(27.701853, 85.319418) // Output the results echo "Nearest vertex of the polygon: " . json_encode($nearestVertex) . PHP_EOL; echo "Is inside: " . ($isInside ? "Yes" : "No") . PHP_EOL; echo "Area: " . $area . PHP_EOL; ?> For circles: <?php // Create a ShapeFactory instance $shape = new ShapeFactory(); // Create a circle shape with center coordinates and radius $drawShape = $shape::create('circle', null, 27.710258, 85.279664, 10); // radius=10 in km // Check if a point is inside the circle $isInside = $drawShape->contains(27.710258, 85.279664); // Calculate the area of the circle $area = $drawShape->area(); // area in sq. km // Output the results echo "Is inside: " . ($isInside ? "Yes" : "No") . PHP_EOL; echo "Area: " . $area . PHP_EOL; ?>;