如何在php中阅读下面的kml文件?

时间:2015-11-14 18:59:17

标签: php

<?xml version='1.0' encoding='UTF-8'?>
<kml xmlns='http://www.opengis.net/kml/2.2'>
    <Document>
        <name></name>
        <Placemark>
            <name></name>
            <description><![CDATA[description: <br><br>WardId: 4<br>Shape_Leng: 7103.25259602000<br>Shape_Le_1: 7103.25259602000<br>Shape_Area: 1374396.12779999990]]></description>
            <styleUrl>#poly-FFFFFF-1-76</styleUrl>
            <ExtendedData>
                <Data name='description'>
                    <value></value>
                </Data>
                <Data name='ULBName'>
                    <value></value>
                </Data>
                <Data name='WardId'>
                    <value>4</value>
                </Data>
                <Data name='Shape_Leng'>
                    <value>7103.25259602000</value>
                </Data>
                <Data name='Shape_Le_1'>
                    <value>7103.25259602000</value>
                </Data>
                <Data name='Shape_Area'>
                    <value>1374396.12779999990</value>
                </Data>
            </ExtendedData>
            <Polygon>
                <outerBoundaryIs>
                    <LinearRing>
                        <tessellate>1</tessellate>
                        <coordinates>74.993243,15.488983,0.0 74.99246,15.489756,0.0 74.992439,15.488891,0.0 74.992614,15.488423,0.0 74.992705,15.487784000000001,0.0 74.992835,15.487309999999999,0.0 74.992879,15.486597999999999,0.0 74.992903,15.486198000000002,0.0 74.993204,15.485721000000002,0.0 74.993615,15.485400000000002,0.0 74.993974,15.485028999999997,0.0 74.994503,15.484024,0.0 74.994763,15.483179000000002,0.0 74.995571,15.482164999999998,0.0 74.99618,15.481296000000002,0.0 74.996778,15.480464,0.0 74.997053,15.480071000000002,0.0 74.99758900000002,15.478965000000002,0.0 74.997966,15.478071,0.0 74.998122,15.477612000000002,0.0 74.998578,15.476816,0.0 74.998876,15.475947,0.0 74.998887,15.475911,0.0 74.998978,15.475445999999996,0.0 74.999091,15.475168,0.0 74.999163,15.474965,0.0 74.99934,15.474301,0.0 74.999432,15.473543,0.0 74.999474,15.473187999999999,0.0 74.999637,15.472535,0.0 74.999756,15.472109000000001,0.0 74.99968,15.471658999999999,0.0 74.999646,15.471458,0.0 74.999566,15.470972,0.0 74.99978,15.47094,0.0 74.999713,15.470204,0.0 74.999676,15.469762999999999,0.0 74.999669,15.469649,0.0 75.000343,15.469477000000005,0.0 75.000386,15.467789999999997,0.0 75.001133,15.468036,0.0 75.001824,15.468117999999999,0.0 75.001876,15.467829000000002,0.0 75.002009,15.466991000000002,0.0 75.002342,15.466923000000001,0.0 75.002689,15.466886000000004,0.0 75.003081,15.466824,0.0 75.00374100000002,15.466632999999998,0.0 75.004185,15.466250999999996,0.0 75.00425,15.46577,0.0 75.004122,15.46541,0.0 75.003779,15.465175,0.0 75.00412,15.464468999999996,0.0 75.004419,15.464796,0.0 75.00495,15.464961,0.0 75.005435,15.465155000000001,0.0 75.005896,15.46541,0.0 75.00613000000001,15.465639,0.0 75.006463,15.466240000000003,0.0 75.006823,15.466719999999999,0.0 75.007094,15.467111,0.0 75.007373,15.467509999999999,0.0 75.008011,15.467647000000001,0.0 75.008061,15.467647000000001,0.0 75.00812400000001,15.468452999999998,0.0 75.008141,15.469336,0.0 75.00763,15.470072,0.0 75.007137,15.47082,0.0 75.006886,15.471011999999998,0.0 75.006409,15.471315,0.0 75.006011,15.471806000000003,0.0 75.005672,15.472582999999998,0.0 75.005507,15.473005,0.0 75.005142,15.473607999999997,0.0 75.004826,15.474166000000002,0.0 75.00473,15.474329000000001,0.0 75.004472,15.475067,0.0 75.004179,15.475801,0.0 75.003901,15.476478,0.0 75.003584,15.47724,0.0 75.003479,15.477498999999996,0.0 75.003011,15.478221999999999,0.0 75.002195,15.479398,0.0 75.001887,15.479836,0.0 75.00113,15.480661,0.0 75.000471,15.481472,0.0 74.99978,15.482393000000004,0.0 74.999277,15.483463,0.0 74.9989,15.483996,0.0 74.998416,15.48446,0.0 74.997055,15.485603000000003,0.0 74.996053,15.486444999999996,0.0 74.994457,15.487810999999999,0.0 74.993243,15.488983,0.0</coordinates>
                    </LinearRing>
                </outerBoundaryIs>
            </Polygon>
        </Placemark>
        <Placemark>
            <name></name>
            <description><![CDATA[description: <br>ULBName: <br>WardId: 7<br>Shape_Leng: 5020.58225189000<br>Shape_Le_1: 5020.58225189000<br>Shape_Area: 1068302.69824999990]]></description>
            <styleUrl>#poly-FFFFFF-1-76</styleUrl>
            <ExtendedData>
                <Data name='description'>
                    <value></value>
                </Data>
                <Data name='ULBName'>
                    <value></value>
                </Data>
                <Data name='WardId'>
                    <value>7</value>
                </Data>
                <Data name='Shape_Leng'>
                    <value>5020.58225189000</value>
                </Data>
                <Data name='Shape_Le_1'>
                    <value>5020.58225189000</value>
                </Data>
                <Data name='Shape_Area'>
                    <value>1068302.69824999990</value>
                </Data>
            </ExtendedData>
            <Polygon>
                <outerBoundaryIs>
                    <LinearRing>
                        <tessellate>1</tessellate>
                        <coordinates>75.02649,15.469437999999998,0.0 75.026515,15.469663000000002,0.0 75.026304,15.470367,0.0 75.026349,15.470820999999997,0.0 75.026183,15.471789000000001,0.0 75.025811,15.473340999999996,0.0 75.027386,15.474036,0.0 75.027359,15.474757999999998,0.0 75.027474,15.474835,0.0 75.027275,15.475308999999998,0.0 75.027236,15.476292000000003,0.0 75.027293,15.47772,0.0 75.025557,15.477613,0.0 75.024157,15.477534000000002,0.0 75.022646,15.477439000000002,0.0 75.021019,15.477343000000001,0.0 75.020905,15.477070999999997,0.0 75.020736,15.47664,0.0 75.020603,15.476312000000002,0.0 75.02015,15.4752,0.0 75.019859,15.474506,0.0 75.019541,15.473855,0.0 75.019164,15.473109999999998,0.0 75.018845,15.472477,0.0 75.018487,15.471779,0.0 75.018103,15.471041,0.0 75.017942,15.470742000000003,0.0 75.01803,15.470278,0.0 75.017949,15.469809999999997,0.0 75.017876,15.469402,0.0 75.017881,15.468906999999996,0.0 75.017354,15.469003,0.0 75.016746,15.469030999999996,0.0 75.016084,15.468434999999996,0.0 75.015589,15.467982,0.0 75.015266,15.467591000000002,0.0 75.014416,15.4666,0.0 75.014105,15.466134999999998,0.0 75.013931,15.465893,0.0 75.014657,15.465376,0.0 75.015267,15.464979000000001,0.0 75.015429,15.464926000000002,0.0 75.01557400000002,15.464778999999997,0.0 75.015748,15.464669,0.0 75.01596100000002,15.464483000000003,0.0 75.016017,15.464439,0.0 75.01605500000001,15.464410000000003,0.0 75.016089,15.464384000000003,0.0 75.016129,15.464352,0.0 75.016168,15.464322000000003,0.0 75.016206,15.464292000000004,0.0 75.016251,15.464257,0.0 75.016293,15.464226000000002,0.0 75.016332,15.464197,0.0 75.016398,15.464143999999997,0.0 75.016685,15.463924,0.0 75.01677,15.463857000000003,0.0 75.016788,15.463843,0.0 75.016849,15.463924,0.0 75.016919,15.464021,0.0 75.01694,15.464050000000002,0.0 75.016961,15.464075000000001,0.0 75.017007,15.464140000000004,0.0 75.017057,15.464206999999998,0.0 75.017144,15.464325999999998,0.0 75.017321,15.464365,0.0 75.017422,15.464388000000003,0.0 75.017533,15.464412999999999,0.0 75.017766,15.464465,0.0 75.018056,15.464544999999996,0.0 75.018133,15.464567999999998,0.0 75.018274,15.464633000000001,0.0 75.018477,15.464728999999998,0.0 75.018585,15.464784,0.0 75.018845,15.464859,0.0 75.019228,15.464975999999998,0.0 75.019547,15.465077,0.0 75.019582,15.465134,0.0 75.019637,15.465222000000002,0.0 75.019679,15.46529,0.0 75.019727,15.465370000000002,0.0 75.01978,15.465456000000001,0.0 75.019804,15.465566,0.0 75.019825,15.465695000000002,0.0 75.019856,15.465864999999999,0.0 75.01988,15.465990000000001,0.0 75.019896,15.466067,0.0 75.01991000000001,15.466167999999998,0.0 75.019924,15.466252,0.0 75.019944,15.466378000000002,0.0 75.019965,15.466536999999997,0.0 75.019976,15.466635999999994,0.0 75.020012,15.466947000000003,0.0 75.020028,15.467083,0.0 75.020282,15.467033000000002,0.0 75.020654,15.46696,0.0 75.020722,15.466947000000003,0.0 75.020941,15.467087,0.0 75.021225,15.467269999999996,0.0 75.021411,15.467389000000002,0.0 75.021651,15.467497,0.0 75.021922,15.467617,0.0 75.022211,15.467719000000002,0.0 75.022526,15.467833999999996,0.0 75.022696,15.467896,0.0 75.022909,15.468051000000004,0.0 75.023152,15.468224999999997,0.0 75.023321,15.468343999999998,0.0 75.023598,15.468437000000002,0.0 75.023915,15.468544999999999,0.0 75.024174,15.468661000000003,0.0 75.024516,15.468807000000002,0.0 75.024832,15.468939,0.0 75.024938,15.468983,0.0 75.02519600000001,15.469056,0.0 75.025543,15.469156999999997,0.0 75.025901,15.469264000000004,0.0 75.02618,15.469345999999998,0.0 75.02649,15.469437999999998,0.0</coordinates>
                    </LinearRing>
                </outerBoundaryIs>
            </Polygon>
        </Placemark>
</Document>
</kml>

我想从kml文件上面读取坐标,并使用php将其存储在数组中。然后使用该数组我想分别存储纬度和经度。给定纬度和经度点作为输入,我必须找到它是否属于特定的地方标记。

我的想法是,如果我能够读取坐标我有一个代码告诉天气该点属于多边形或不代码是:

<?php
//Point-In-Polygon Algorithm
$polySides  = 4; //how many corners the polygon has
$polyX    =  array(4,9,11,2);//horizontal coordinates of corners
$polyY    =  array(10,7,2,2);//vertical coordinates of corners
$x = 4;
$y = 10.5;//Outside
//$y = 3.5;//Inside

function pointInPolygon($polySides,$polyX,$polyY,$x,$y) {
  $j = $polySides-1 ;
  $oddNodes = 0;
  for ($i=0; $i<$polySides; $i++) {
    if ($polyY[$i]<$y && $polyY[$j]>=$y 
 ||  $polyY[$j]<$y && $polyY[$i]>=$y) {
    if ($polyX[$i]+($y-$polyY[$i])/($polyY[$j]-$polyY[$i])*($polyX[$j]-$polyX[$i])<$x)    {
    $oddNodes=!$oddNodes; }}
   $j=$i; }

  return $oddNodes; }


 if (pointInPolygon($polySides,$polyX,$polyY,$x,$y)){
  echo "Is in polygon!";
}
else echo "Is not in polygon";
?>

请告诉我天气这种方法是正确的还是有任何API读取kml文件并给出关于给定(输入)纬度和经度属于哪个地标的输出。

2 个答案:

答案 0 :(得分:2)

我认为它们是存储在字符串$kml中的x,y坐标:

$doc   = new DOMDocument();
$doc->loadXML( $kml );
$coordinates = array();
foreach( $doc->getElementsByTagName( 'coordinates' ) as $coord ) {
    foreach( explode( ' ', $coord->nodeValue ) as $c ) {
        list( $x, $y ) = explode( ',', $c );
        $t = new StdClass;
        $t->x = $x;
        $t->y = $y;
        $coordinates[] = $t;
    }
}

var_dump( $coordinates );
// First x is $coordinates[0]->x
// First y is $coordinates[0]->y

坐标作为对象存储在数组$coordinates中。像这样echo "x:" . $coordinates[0]->x . " y:" . $coordinates[0]->y . "<br>";

阅读它们

修改这适用于地标:

$doc   = new DOMDocument();
$doc->loadXML( $kml );
$placemark = array();

foreach( $doc->getElementsByTagName( 'Placemark' ) as $place ) {
    $coordinates = array();
    foreach( $doc->getElementsByTagName( 'coordinates' ) as $coord ) {
        foreach( explode( ' ', $coord->nodeValue ) as $c ) {
            list( $x, $y ) = explode( ',', $c );
            $t = new StdClass;
            $t->x = $x;
            $t->y = $y;
            $coordinates[] = $t;
        }
    }
    $placemark[] = $coordinates;
}

var_dump( $placemark );
// First placemarks first x is $placemark[0][0]->x
// First placemarks first y is $placemark[0][0]->y

答案 1 :(得分:0)

考虑到我的评论,您可以使用以下代码作为起点:

$xml = simplexml_load_file('test.xml');
echo $xml->Document->Placemark->Polygon->outerBoundaryIs->LinearRing->coordinates->__toString();

根据您想要对坐标执行的操作,您可以使用explode()

编辑:要检索所有坐标,您必须循环遍历它们,如下所示:

$xml = simplexml_load_file('test.xml');
foreach ($xml->Document->Placemark as $placemark) {
    $coordinates = $placemark->Polygon->outerBoundaryIs->LinearRing->coordinates->__toString();
    echo $coordinates; // gives out the actual coordinates
}