GIS and mapping in PHP

GIS and mapping in PHP
Content List
This post is the first in a series that will provide readers some advice on how to build a simple GIS system on your own for no cost. Although I don't profess to be an expert in GIS and there may be more effective approaches, hopefully these articles will point you in the right path.

Many excellent online GIS (Geographic Information Systems) solutions are available, like Mappoint, Mapquest, and other interesting PHP projects. It is nevertheless good to be able to plot your own data and add some simple mapping tools to your website with the least amount of hassle.

Plotting a single spot on a world map that has already been drawn will be our first step in this article. Using the concepts presented in this article, subsequent topics will cover labelling items, sketching highways and rivers, and creating vector maps.

GIS and mapping in PHP


What you'll need for this article is:

PHP and installed GD

scissors, a pair

Adult assistance for you

being able to find your position in this planet

An object must be geocoded, or assigned a map coordinate, in order to be plotted on any map, whether it is a house or a herd of roasted Wombats. A typical longitude (long) and latitude (lat) coordinate was used for each case. There are many different coordinate systems available, some far more exact, but this one is the most typical, the simplest to display, and it works for our purposes. Directly charting long/lat coordinates has the drawback of distorting as you move closer to the poles. This doesn't present a difficulty for our purposes, though.

The angle in degrees that the two coordinates of a longitude and latitude from the equatorial plane of the planet are measured. Latitude lines provide the "Y" coordinate, whereas longitude lines, which run from pole to pole, provide the "X" coordinate. These can be expressed as degrees, minutes, and seconds (D'M'S) or as a decimal value (e.g., 23.323232). We used the decimal form of our coordinates for storage because it is much simpler and takes much less time to process. Numerous websites offer Javascript tools for converting coordinates from the D'M'S (degrees, minutes, and seconds) format to the decimal format. The function is incorporated into the scientific calculator I use, a Sharp EL-546.

There are several methods for determining a point's longitude and latitude, however using Microsoft's online GIS tool Mappoint is a quick and simple method ( www.mappoint.com ). Make sure the location you choose is in the middle of the map by utilising Mappoint's search tools. Long/lat coordinates are included in the URL of the map image when you right-click it. Additionally, a number of government-run environmental websites will have map data that you may download for free. It is also worthwhile to visit www.geogratis.com. On the sample discs that come with Mapinfo, those of you who own it, will also discover really helpful geocoded data.

My 64-bedroom home, which is situated in the bustling urban metropolis of Prince Edward Island, Canada, is the location we are plotting for this essay. I found my location using Mappoint to be:

Longitude: -63.10774861954596
Latitude: 46.2899306519141

To lay out our argument, we now require a basis map. We'll generate this ourselves in later articles, but for now we'll utilise a plain pre-drawn JPG image. Earth 310.jpg, the basis map that we are using, is a view of the earth that is known as a cylindrical projection.

The original of this picture can be found online in a variety of sizes and scales. The simplest projection to map long/lat coordinates onto requires the least amount of conversion, which minimises processor overhead. On save the image to your hard drive for our needs, simply right-click the image.

For convenience, the base map is sized to 310155, however you can resize this map to any size.

to produce the code necessary to plot our point.

Plans and ruses

The map is created using the following basic steps:

Get the background map loaded.

The long/lat coordinates should be scaled and converted to screen coordinates.

Plan your point.

Send back the completed map as an image.

We have developed a function called getlocationcoords to translate the long/lat to screen coordinate. This returns the screen coordinates in an associative array together with the longitude, latitude, and base map size. The size of the background image is used to compute $width and $height. These variables will be used to scale the map and control the zoom level in next projects.

Using free, available GIS data, you may add digital maps to websites using the PEAR tool for PHP called Image GIS. Find out how to integrate mapping features into a website by using the Image GIS package.

Using free GIS files that can be downloaded that contain latitude and longitude data about specific geographic places, developers can add digital maps to their websites using the PEAR package for PHP called Image GIS. In this article, I'll show you how to integrate mapping capabilities into a website using the Image GIS package.

The Image GIS package must be installed using PEAR in order to be used. The addDataFile() method of the Image GIS module allows E00 format input via an.e00 file. The data contained in the file is parsed as it is added; parsing entails reading each line of the input file, interpreting the data, and saving the data locally. The showImage() method can then be used to render the data as a PNG image when the information has been parsed. Latitude and longitude point-to-point data are used to build lines and produce the final image.

Installing the Image GIS package is the initial step in developing this solution. You must find a file with GIS data in ARC/INFO (.e00) format after installing Image GIS and its dependencies. Fortunately, GIS data downloads in ARC/INFO format are available on the website of the U.S. Census Bureau. I'll be using Kentucky county border data, the state where I now live, in this solution.

The fact that the Image GIS programme can only interpret single-precision data and the format of the data in these files is a barrier. Although a little coding adjustment is required in order to accept double-precision data, PHP is remarkable in that the source code is included in the package. Take these actions:

Find the E00.php file in the Image GIS package installation directory.

Find the following regular expression search pattern by opening this file:

([ -][0-9]\.[0-9]{7}E[-+][0-9]{2})

Change the search pattern's 7-digit restriction to a 7- to 14-digit restriction, as in the following:

([ -][0-9]\.[0-9]{7,14}E[-+][0-9]{2})

The Image GIS package's expectation that the point-to-point data finish when the file does is another issue. However, this is not the case in the U.S. Census Bureau data; the point-to-point information ceases when the line-set identifier switches to -1. Label information follows this specific line in the input file. All that is required of us is to look for this pattern in the input file and stop parsing when we find this line:

<?php
function getlocationcoords($lat, $lon, $width, $height)
{
$x = (($lon + 180) * ($width / 360));
$y = ((($lat * -1) + 90) * ($height / 180));
return array(“x”=>round($x),”y”=>round($y));
}
?>

Once the coordinates have been converted it’s as simple as drawing a rectangle on the base map using the returned array to mark our location.

So, the code to create the map:

<?php
// These are the coordinates the location we wish to plot.
// These are being passed in the URL, but we will set them to a default if nothing is passed.
if(empty($long))$long = -63.10774861954596;
if(empty($lat)) $lat = 46.2899306519141;
// First we load the background/base map. We assume it’s located in same dir as the script.
// This can be any format but we are using JPG in this example
// We will also allocate the color for the marker
$im = imagecreatefromjpeg(“earth_310.jpg”);
$red = imagecolorallocate ($im, 255,0,0);
// Next need to find the base image size.
// We need these variables to be able scale the long/lat coordinates.
$scale_x = imagesx($im);
$scale_y = imagesy($im);
// Now we convert the long/lat coordinates into screen coordinates
$pt = getlocationcoords($lat, $long, $scale_x, $scale_y);
// Now mark the point on the map using a red 4 pixel rectangle
imagefilledrectangle($im,$pt[“x”]-2,$pt[“y”]-2,$pt[“x”]+2,$pt[“y”]+2,$red);
// Return the map image. We are using a PNG format as it gives better final image quality than a JPG
header (“Content-type: image/png”);
imagepng($im);
imagedestroy($im);
?>

To load the map call the PHP code from an IMG tag, passing the longitude and latitude in the long and lat variables:

<IMG SRC=”map_code.php?long=long&lat=lat“>

To read long/lat coordinates from a database or a delimited text file and plot many points, you might easily alter this code.

else if ($numRecords == 0 &&
preg_match("#^\s+(-1)\s+([-0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0
-9]+)\s+([0-9]+)\s+([0-9]+)#", $line, $a)) {
if ($this->debug) {
echo $line . '<br />';
}
break;}

Once we make these changes, we can use the package. The

documentation on this package is quite limited, but there is an example at the

beginning of the GIS.php file. This example maps out

the hometown of one of the Image_GIS package

developers. Using this example, I can create my own code to generate the PNG

image of Kentucky:

<?php
require_once "Image/GIS.php";
$map = new Image_GIS(
array(
'width' => 1200,
'height' => 600,
'debug' => 0
)
);
$map->addDataFile('X:\path_to_file\co21_d00.e00', 'black');
$map->showImage();
?>

The first thing I do is require the Image_GIS

Summary

This script is pretty simple. However, using the fundamental concept will enable you to create extremely intricate maps.

The getlocationcoords function is crucial; once you've geocoded your data and have the tools to plot the points on the screen, the possibilities are endless!

Who the Author Is

A collection of articles on GIS/mapping in PHP written by Simon Moss are available at www.web-max.ca/PHP. Visit http://www.staycanada.ca to see some wonderful examples of this technology in action.

package. Then I use the function Object() { [native code] } information to create an instance of the Image GIS class. Debugging just exposes verbose information as the data is being processed and produced, thus in this example, I specify the width and height of the final image then turn it off. The addDataFile() method is used to add the data file once the instance has been created. The colour given in the method call's second parameter is used to draw the lines. The showImage() method then uses the imagepng() method of the PHP imaging library to output the image as a PNG image to the response.

To map several regions within the same image, you can add more than one GIS file and select different colours.

Open the GIS file and take a peek within it once you've gotten the hang of writing this solution. You'll observe that each set is a collection of connected lines. In lat/long coordinates, each data line corresponds to a line on the generated image. You can make your own GIS file to add to your image once you have a grasp of this file. If you have a map or satellite image that is constrained by lat/long coordinates, this device may be helpful. The resultant image will display the positions of your mapped regions when the map image is overlaid with the new image produced by the Image GIS programme.

Download the source code to see a functional example.

By automatically subscribing to TechRepublic's free Web Development Zone newsletter, which is sent out every Tuesday, you can keep your developer skills sharp.

A few remarks regarding the job of PHP developers

Our current era is very focused on the internet. Thus, there is a higher than ever need for software solutions. The digital experts who can meet those needs and propel us even further into the future are PHP developers. Consequently, practically every company and private service requires developers. We will provide some insight into these engineers' work in this article.

The language a web developer uses to programme

The scripting language PHP, which stands for Hypertext Preprocessor, is used to create real-time programmes that are intended to operate over the Internet. PHP programmers are able to create scripts with a very broad range of useful capabilities. PHP programming is required for all contemporary web development. Therefore, hiring a PHP developer is essential if this procedure is to satisfy any kind of modern project needs.

A skilled software developer can also strike a balance between several HTML and JavaScript frameworks in addition to all of the above. A full-stack developer is someone who is capable of carrying out such tasks. In other words, it's a person who is proficient in a variety of programming languages, including PHP, of course.

Other qualities and conduct a PHP developer should display

Fluency in a programming language is simply one of the qualifications needed to work for a respectable software company in the field of computer science. Even if a lone wolf type has years of expertise developing apps and has received good client feedback, they are unlikely to be able to handle the upcoming problems. This is due to the fact that modern software development methods are extremely complex and constantly evolving.

As a result, the capacity for collaboration and work sharing with other programmers is more than necessary. Beyond fundamental everyday language, communication skills are also important. One must comprehend the meaning of teamwork in order to operate as a developer with a software firm on a permanent basis. The likelihood of success in this industry is low for someone who wants to work alone.

The hint

Success as a PHP developer is not simple, that much is true. However, as we already indicated, there is currently a significant need for this occupation. It may be quite successful. Make sure to concentrate heavily on mobile apps if you wish to join the ranks of PHP developers. Our culture is moving in this direction. Even the most complex programmes will soon be available in a mobile format that can be utilised on tablets, which are currently cutting-edge tech devices with strong capabilities.