Giter Club home page Giter Club logo

php-osm-static-api's Introduction

Packagist Latest Stable Version GitHub license

PHP OpenStreetMap Static API

PHP library to easily get static image from OpenStreetMap with markers, lines, circles and polygons.

This project uses the Tile Server of the OpenStreetMap Foundation which runs entirely on donated resources, see Tile Usage Policy for more information.

✨ Supporting

⭐ Star this repository to support this project. You will contribute to increase the visibility of this library 🙂

Installation

Install this library easily with composer :

composer require dantsu/php-osm-static-api

How to use

Generate OpenStreetMap static image with markers and polygon :

use \DantSu\OpenStreetMapStaticAPI\OpenStreetMap;
use \DantSu\OpenStreetMapStaticAPI\LatLng;
use \DantSu\OpenStreetMapStaticAPI\Polygon;
use \DantSu\OpenStreetMapStaticAPI\Markers;

\header('Content-type: image/png');
(new OpenStreetMap(new LatLng(44.351933, 2.568113), 17, 600, 400))
    ->addMarkers(
        (new Markers(__DIR__ . '/resources/marker.png'))
            ->setAnchor(Markers::ANCHOR_CENTER, Markers::ANCHOR_BOTTOM)
            ->addMarker(new LatLng(44.351933, 2.568113))
            ->addMarker(new LatLng(44.351510, 2.570020))
            ->addMarker(new LatLng(44.351873, 2.566250))
    )
    ->addDraw(
        (new Polygon('FF0000', 2, 'FF0000DD'))
            ->addPoint(new LatLng(44.351172, 2.571092))
            ->addPoint(new LatLng(44.352097, 2.570045))
            ->addPoint(new LatLng(44.352665, 2.568107))
            ->addPoint(new LatLng(44.352887, 2.566503))
            ->addPoint(new LatLng(44.352806, 2.565972))
            ->addPoint(new LatLng(44.351517, 2.565672))
    )
    ->getImage()
    ->displayPNG();

Exported OpenStreetMap image

Align and zoom the map to drawings and markers :

  • ->fitToDraws(int $padding = 0)
  • ->fitToMarkers(int $padding = 0)
  • ->fitToDrawsAndMarkers(int $padding = 0)
  • ->fitToPoints(LatLng[] $points, int $padding = 0)

$padding sets the amount of padding in the borders of the map that shouldn't be accounted for when setting the view to fit bounds. This can be positive or negative according to your needs.

use \DantSu\OpenStreetMapStaticAPI\OpenStreetMap;
use \DantSu\OpenStreetMapStaticAPI\LatLng;
use \DantSu\OpenStreetMapStaticAPI\Polygon;
use \DantSu\OpenStreetMapStaticAPI\Markers;

\header('Content-type: image/png');
(new OpenStreetMap(new LatLng(0, 0), 0, 600, 400))
    ->addMarkers(
        (new Markers(__DIR__ . '/resources/marker.png'))
            ->setAnchor(Markers::ANCHOR_CENTER, Markers::ANCHOR_BOTTOM)
            ->addMarker(new LatLng(44.351933, 2.568113))
            ->addMarker(new LatLng(44.351510, 2.570020))
            ->addMarker(new LatLng(44.351873, 2.566250))
    )
    ->addDraw(
        (new Polygon('FF0000', 2, 'FF0000DD'))
            ->addPoint(new LatLng(44.351172, 2.571092))
            ->addPoint(new LatLng(44.352097, 2.570045))
            ->addPoint(new LatLng(44.352665, 2.568107))
            ->addPoint(new LatLng(44.352887, 2.566503))
            ->addPoint(new LatLng(44.352806, 2.565972))
            ->addPoint(new LatLng(44.351517, 2.565672))
    )
    ->fitToDraws(10)
    ->getImage()
    ->displayPNG();

Documentation

Class Description
Circle DantSu\OpenStreetMapStaticAPI\Circle draw circle on the map.
LatLng DantSu\OpenStreetMapStaticAPI\LatLng define latitude and longitude for map, lines, markers.
Line DantSu\OpenStreetMapStaticAPI\Line draw line on the map.
MapData DantSu\OpenStreetMapStaticAPI\MapData convert latitude and longitude to image pixel position.
Markers DantSu\OpenStreetMapStaticAPI\Markers display markers on the map.
OpenStreetMap DantSu\OpenStreetMapStaticAPI\OpenStreetMap is a PHP library created for easily get static image from OpenStreetMap with markers, lines, polygons and circles.
Polygon DantSu\OpenStreetMapStaticAPI\Polygon draw polygon on the map.
TileLayer DantSu\OpenStreetMapStaticAPI\TileLayer define tile server url and related configuration
XY DantSu\OpenStreetMapStaticAPI\XY define X and Y pixel position for map, lines, markers.

Contributing

Please fork this repository and contribute back using pull requests.

Any contributions, large or small, major features, bug fixes, are welcomed and appreciated but will be thoroughly reviewed.

php-osm-static-api's People

Contributors

cquest avatar dantsu avatar davetha avatar mandrasch avatar stephan-strate avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

php-osm-static-api's Issues

make sure markers are same distance borders

rhederlaag_840

I would like to "shift" the map so the marker bounds are all from the same distance from the borders.
Right now the points seem to be used. However, it makes more sense if the "top" of the marker is used, when markers have a different anchor point:

$marker->setAnchor(Markers::ANCHOR_CENTER, Markers::ANCHOR_BOTTOM);

PHP code example produces tiny image

Hello,
When running the example code it presents a tiny image in the centre of the screen, approx 10 pixels wide.
I have tried adjusting the scaling but nothing changes.
Thanks

Projection of lat lon

Great Job!

What is the projection of the lat lon variables?

I have tried to put my town (Burriana/Castellon/Spain) and I see my marker on Africa

Thank you

I'm able to draw shapes but base map doesn't show!

Initially when I first used this package it worked just fine with any coordinates, but I had to update my php version to 8 to accommodate other needs of my project and I'm not sure if that has anything to do with this but it now has stopped working like before mainly that the base map(tileServer?) doesn't show up anymore but the drawing of the spatial shapes still functions and the display/save options work as well!

Here's how it looks:
test

Draw circle on map

Would it be possible to draw a circle on the map, like in this example:

Screenshot 2022-05-17 at 18 10 05

I saw that your Image class in php-image-editor has a drawCircle method, but I'm not sure how to use it to draw on a map image.
Thanks for any pointers.

Wrong marker position on smaller zoom numbers

Thank you very much for this fine library.

It seems like a marker it getting shifted up the smaller the zoom is, i.e. for zoom 12 the marker position seems good. If I decrease the zoom stepwise to smaller numbers, the marker will be positioned above (and more left) of the given coordinates.

In the following images the zoom is decreased from 12 to 8, 6 and 4.
At zoom 4 the marker is almost at germanys norther border, but the coordinates point to a place way south.

db7b1e5f-a6db-4942-9ee7-f1f59f245842
efddea52-d2e2-461e-849d-865ff37e0303
f68da997-483f-48b0-9767-d4921d17a078
4eac9f48-f4e7-4e88-920f-a14867c77152

My setup is rather simple

$tempFilePath = tempnam(sys_get_temp_dir(), 'osm');
$markerPath = $this->parameterBag->get('kernel.project_dir').'/assets/images/marker-black.png';

$markers = (new Markers($markerPath))
    ->setAnchor(Markers::ANCHOR_CENTER, Markers::ANCHOR_BOTTOM)
    ->addMarker(new LatLng($lat, $lng));

$map = (new OpenStreetMap(new LatLng($lat, $lng), $zoom, 640, 640))
    ->addMarkers($markers);

$map->getImage()->savePNG($tempFilePath);

I had a quick look at Markers::draw() but could not find the problem.

Edit: The marker anchor position does not seem to have anything to do with the misplacement.

default zoomlevel, fittomarkers zoom out

I'm using fit to markers, to make sure all the markers fit on the map.
However, the function zoom as much in (higher number) so markers fit in the image.

I would like to have a preferred zoomlevel and if the markers don't fit in, then to zoom out (but not the other way round).

Is there a way to accomplish this?

[Feature Request] Caching ability

It seems the server tries to download the image each time, which makes the page load slower. Would it perhaps be possible to keep the current image in a cache and only refresh once per day?

Test server connectivity

Hi,

A fantastic library. Got code set-up and running, downloads and saves as a JPG. Works perfect, but just testing a few scenarios. If for whatever reason the tile server is unreachable the image saves as one big black square. Which is fine, it isn't loading anything. But was wondering if there is a way to do a quick check that the tile server can be reached before building the map and saving it?

Thanks?

getImage function throws expectation error

getImage function

I have a utilized the getImage function in a feature that I have on a personal project and I noticed recently it's throwing an error!

Let me know if you need anything else to debug this issue.

Stack Trace:

ErrorException: imagecreatefromstring(): Data is not in a recognized format in ...\vendor\dantsu\php-image-editor\src\Image.php:245
#1 ...\vendor\dantsu\php-image-editor\src\Image.php(245): imagecreatefromstring()
#2 ...\vendor\dantsu\php-image-editor\src\Image.php(233): DantSu\PHPImageEditor\Image->data()
#3 ...\vendor\dantsu\php-image-editor\src\Image.php(332): DantSu\PHPImageEditor\Image::fromData()
#4 ...\vendor\dantsu\php-image-editor\src\Image.php(296): DantSu\PHPImageEditor\Image->curl()
#5 ...\vendor\dantsu\php-osm-static-api\src\TileLayer.php(198): DantSu\PHPImageEditor\Image::fromCurl()
#6 ...\vendor\dantsu\php-osm-static-api\src\OpenStreetMap.php(217): DantSu\OpenStreetMapStaticAPI\TileLayer->getTile()
#7 ...\vendor\dantsu\php-osm-static-api\src\OpenStreetMap.php(271): DantSu\OpenStreetMapStaticAPI\OpenStreetMap->getMapImage()
#8 ...\app\MY_FILE_CALL: DantSu\OpenStreetMapStaticAPI\OpenStreetMap->getImage()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.