GIS spatial functions in PHP? - php

I need to convert data points from one geographic projection (Lat Long, Mercator, UTM) to another and I wonder if there's a set of PHP tools or functions that can do this? I tried writing one myself based on formulas I found, but it wasn't accurate enough and I can't find better formulas anywhere, so I was wondering if there might be some prepackaged functions somewhere. Failing that, what about something like PROJ.4? Thanks!

There is a PHP module of Proj4 available in the MapServer/MapScript distribution. I think it is mantanied by DM Solutions, but I could not find any documentation online. To check the available functions, I had to look at the source code.
Anyway, this is how you can tranform coordinates between projections:
<?php
//UTM zone 31N
$projDefSrc = array("proj=utm","zone=31","ellps=intl","units=m","no_defs");
$pjSrc = pj_init($projDefSrc);
//WGS84
$projDefDest = array("proj=longlat","ellps=WGS84","datum=WGS84","no_defs");
$pjDest = pj_init($projDefDest);
$x = 446423;
$y = 4610005;
$test = pj_transform($pjSrc,$pjDest,$x,$y);
//Outputs: Array ( [u] => 2.3567240656 [v] => 41.6384346565 )
print_r($test);
?>
If you want to go this way, you will have to compile php_proj.c from the Mapserver source code folder (mapserver-X.X.X/mapscript/php3) and load the extension in PHP. As I said before, there is no documentation online, so let me know if you find any problems.
Hope this helps.

You can use the api proj4php that I have translated from proj4js and is available here : https://sourceforge.net/projects/proj4php/
It works great from WGS84 to Lambert93, but need some fix to work with the others projections. I can help.
Bye.

Can you run ArcGIS Server? ESRI has a new service called a Geometry service that lets you do geometry manipulation/conversion/etc through a variety of service interfaces.
You can find a sample version at http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer that you can test with.

Related

Coordinates: convert from WGS84 to a *specific* UTM-region

There are a lot of free tools out there to convert from UTM to Lat/Long. Fine enough, but I need to go the other way; from WGS-84 to lat/long-format.
But it's more complicated than that; 'couse I need the result to be in UTM-33 (nordic) zone.
This might sound like a bad idea; why would I like to "force" the zone to 33N, when the geographical point might be laying in another zone ...
Well; the thing is that I already have a database with UTM33-coordinates of every address in Norway.
Those of you that are familiar with UTM # Northern Europe, knows that Norway spans across several zones; from 31 to 36.
(Okay, maybe we only spans from 32 to 36, cause of the strange width of zone 32V, but thats another discussion).
So, back to my problem: all my addresses are already given in UTM-33 format (with negative values when out of range). How can i proceed to get my Lat/Long into UTM-33?
I need a solution in PHP, and after a lot of debugging with "gPoint", I found out it just won't work ...
(gPoint is great to convert from/to UTM, but it will always return the UTM x/y-pair in the "correct" zone block. I don't want that! I need to always get results in zone 33, regardless of what is actual correct..)
I finally found a solution though Proj4 (thanx SlightlyCuban)!
// Conversion from WGS84 to UTM33
$proj4 = new Proj4php();
$projWGS84 = new Proj4phpProj('EPSG:4326');
$projUTM33N = new Proj4phpProj('EPSG:2078');
$result = $proj4->transform($projWGS84, $projUTM33N, new proj4phpPoint($long, $lat));
It is impossible to guess correct format for transformation, and I was struggling for a while ... then I discovered a really handy webpage: http://spatialreference.org. Here I found the definition I needed (EPSG:2078 = "+proj=utm +zone=33 +ellps=intl +units=m +no_defs")
The PHP-implementation of Proj4 needs hard-coded definitions; you cannot pass by a definition ad-hoc. EPSG:2078 was originally missing from Proj4php. Yay; lucky me!
I would therefor recommend to test Proj4 via Node.js and proj4js (see my little demo at http://pastebin.com/1BP8cWpj).
To succeed, I had to fork the Proj4PHP-library, and add a definition file for EPSG:2078 ...

String/Paragraph/Document comparison in php

I'm trying to add a feature to generate a difference report between 2 20,000 character sections of text. I've done some Googling and I heard about Pear's diff library - which has been discontinued - and found this: https://github.com/paulgb/simplediff/blob/5bfe1d2a8f967c7901ace50f04ac2d9308ed3169/simplediff.php
Ideally I'd like to see what was removed, edited, or added and be able to show that to the user. Are there any libraries or simple ways of accomplishing this that you may know of?
I use this code in a live project
http://svn.geograph.org.uk/svn/branches/british-isles/libs/3rdparty/simplediff.inc.php
Example use
http://svn.geograph.org.uk/svn/branches/british-isles/public_html/article/diff.php
but the code is very simple
$a1 = explode("\n",$file1);
$a2 = explode("\n",$file2);
print diff2table($a1,$a2);
(the code just accepts the input as arrays, and outputs html table. But diff2table can be customised)

Extract data from HTML in PHP or Python

I need to extract this data and display a simple graph out of it.
Something like Equity Share Capital -> array (30.36, 17, 17 .... etc) would help.
<html:tr>
<html:td>Equity Share Capital</html:td>
<html:td class="numericalColumn">30.36</html:td>
<html:td class="numericalColumn">17.17</html:td>
<html:td class="numericalColumn">15.22</html:td>
<html:td class="numericalColumn">9.82</html:td>
<html:td class="numericalColumn">9.82</html:td>
</html:tr>
How do I go about this task in PHP or Python?
A good place to start looking would be the python module BeautifulSoup which extracts the text and places it into a table.
Assuming you've loaded the data into a variable called raw:
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(raw)
for x in soup.findAll("html:td"):
if x.string == "Equity share capital":
VALS = [y.string for y in x.parent.findAll() if y.has_key("class")]
print VALS
This gives:
[u'30.36', u'17.17', u'15.22', u'9.82', u'9.82']
Which you'll note is a list of unicode strings, make sure to convert them to whatever type you desire before processing.
There are many ways to do this via BeautifulSoup. The nice thing I've found however is the quick hack is often good enough (TM) to get the job done!
BeautifulSoup
Don't forget lxml in Python. It also works well to extract data. It's harder to install but faster. http://pypi.python.org/pypi/lxml/2.2.8

How do I convert html pages into a single .chm file using PHP?

Is there a library for this task?
Does it have to be PHP? If not, you might have a look at
http://en.wikipedia.org/wiki/Microsoft_Compiled_HTML_Help
Look for "Microsoft Help Workshop." ( http://www.microsoft.com/downloads/details.aspx?familyid=00535334-c8a6-452f-9aa0-d597d16580cc&displaylang=en )
A reverse engineering approach is done here: http://www.russotto.net/chm/chmformat.html
So you could look, if you find libraries for the several subproblems.
Check out phpdoc, it can make CHMs
There is no any direct method for PHP reading/writing CHM file.
This may help. :) http://savannah.nongnu.org/projects/hhm

Read in external links in PHP

I know how to do this in Ruby, but I want to do this in PHP. Grab a page and be able to parse stuff out of it.
Take a look at cURL. Knowing about cURL and how to use it will help in many ways as it's not specific to PHP. If you want something specific however, you can use file_get_contents which is the recommended way in PHP to get the contents of a file into a string.
$file = file_get_contents("http://google.com/");
How to parse it depends on what you are trying to do, but I'd recommend one of the XML libraries for PHP.
You could use fopen in read mode: fopen($url, 'r'); or more simply file_get_contents($url);. You could also use readfile(), but file_get_contents() is potentially more efficient and therefore recommended.
Note: these are dependent on config (see the linked manual page) but will work on most setups.
For parsing, simplexml is enabled by default in PHP.
$xmlObject = simplexml_load_string($string);
// If the string was valid, you now have a fully functional xml object.
echo $xmlObject->username;
Its funny, I had the opposite question when I started rails development

Categories