Kirjautuminen

Haku

Tehtävät

Keskustelu: Ohjelmointikysymykset: PHP ja Ilmatieteen laitoksen avoin data?

amuse [19.02.2014 23:42:55]

#

Terve,

Ilmatieteen laitos on näköjään avannut datansa: https://ilmatieteenlaitos.fi/avoin-data.

Onko kukaan käyttänyt tuota dataa php:llä? Data tulee mielestäni aika sekavassa xml-muodossa, enkä ainakaan itse keksinyt äkkiseltään miten tuota olisi kohtuullisen helppoa käsitellä. Jos sen saisi parsittua johonkin taulukkoon, niin siitä varmaan saisi helpoiten tietoja irti...

Esimerkki Helsingin sääennusteesta:
(http://data.fmi.fi/fmi-apikey/OMA-API-AVAIN/wfs?request=getFeature&storedquery_id=fmi::­fo­re­cast::­hir­lam::­sur­fa­ce::­point::­ti­me­va­lu­epair&place=helsinki)

<?xml version="1.0" encoding="UTF-8"?>
<wfs:FeatureCollection
    timeStamp="2014-02-19T21:36:57Z"
    numberMatched="23"
    numberReturned="23"
    xmlns:wfs="http://www.opengis.net/wfs/2.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:om="http://www.opengis.net/om/2.0"
    xmlns:omso="http://inspire.ec.europa.eu/schemas/omso/2.0rc3"
    xmlns:ompr="http://inspire.ec.europa.eu/schemas/ompr/2.0rc3"
    xmlns:gml="http://www.opengis.net/gml/3.2"
    xmlns:gmd="http://www.isotc211.org/2005/gmd"
    xmlns:gco="http://www.isotc211.org/2005/gco"
    xmlns:swe="http://www.opengis.net/swe/2.0"
    xmlns:gmlcov="http://www.opengis.net/gmlcov/1.0"
    xmlns:sam="http://www.opengis.net/sampling/2.0"
    xmlns:sams="http://www.opengis.net/samplingSpatial/2.0"
    xmlns:wml2="http://www.opengis.net/waterml/2.0"
    xmlns:target="http://xml.fmi.fi/namespace/om/atmosphericfeatures/0.95"
    xsi:schemaLocation="http://www.opengis.net/wfs/2.0 http://schemas.opengis.net/wfs/2.0/wfs.xsd
    http://www.opengis.net/gmlcov/1.0 http://schemas.opengis.net/gmlcov/1.0/gmlcovAll.xsd
    http://www.opengis.net/sampling/2.0 http://schemas.opengis.net/sampling/2.0/samplingFeature.xsd
    http://www.opengis.net/samplingSpatial/2.0 http://schemas.opengis.net/samplingSpatial/2.0/spatialSamplingFeature.xsd
    http://www.opengis.net/swe/2.0 http://schemas.opengis.net/sweCommon/2.0/swe.xsd
    http://inspire.ec.europa.eu/schemas/omso/2.0rc3 http://inspire.ec.europa.eu/draft-schemas/omso/2.0rc3/SpecialisedObservations.xsd
    http://inspire.ec.europa.eu/schemas/ompr/2.0rc3 http://inspire.ec.europa.eu/draft-schemas/ompr/2.0rc3/Processes.xsd
    http://www.opengis.net/waterml/2.0 http://inspire.ec.europa.eu/draft-schemas/waterml/2.0rfc/waterml2.xsd
    http://xml.fmi.fi/namespace/om/atmosphericfeatures/0.95 http://xml.fmi.fi/schema/om/atmosphericfeatures/0.95/atmosphericfeatures.xsd">


    <wfs:member>
        <omso:PointTimeSeriesObservation gml:id="WFS-oZdjKUKXhbQN0niaF6ujwpqxOf2JTowtIWbbpdOs2_llx4efR060aeWzDtdOufXlmw48rp1w36d3R066aduXth2dcvDDp5QtLFlz6d1TTty2vvoUTN5_riMbHy51qRaFOO6dNGTVwzsu7JU07ctr76FE1eeYYTgzad2Sdlw8svPpWw7NOSQzM.Xfpyc6zGzVwyZNWlsy9suyp54ZamZs348OzLWpm0340ld16ZnDW24fETTz6Yd2PLStXQgNbbp589O7PUy.OlY07DOZW3fky7KzNGnlsw7WVww8sO2tTPl38NGXTn0dG5z6b.WXJx65eXm_pyV0hZtul06zb.WXHh59HTrRp5bMO10659eWbDjyunXDfp3dHTrpp25e2HZ1y8MOnk0Omnblp9MvCfkJodNO3La37stY1WpDAA---geopheight">
	                     <om:phenomenonTime>
                <gml:TimePeriod gml:id="time-interval-1-1">
                    <gml:beginPosition>2014-02-19T22:00:00Z</gml:beginPosition>
                    <gml:endPosition>2014-02-21T09:00:00Z</gml:endPosition>
                </gml:TimePeriod>
            </om:phenomenonTime>
            <om:resultTime>
                <gml:TimeInstant gml:id="time-1-1">
                    <gml:timePosition>2014-02-19T12:00:00Z</gml:timePosition>
                </gml:TimeInstant>
            </om:resultTime>

	    <om:procedure xlink:href="http://xml.fmi.fi/inspire/process/hirlam"/>
	                <om:parameter>
                <om:NamedValue>
                    <om:name xlink:href="http://inspire.ec.europa.eu/codeList/ProcessParameterValue/value/numericalModel/analysisTime"/>
                    <om:value>
                        <gml:TimeInstant gml:id="analysis-time-1-1-geopheight--">
                            <gml:timePosition>2014-02-19T12:00:00Z</gml:timePosition>
                        </gml:TimeInstant>
                    </om:value>
                </om:NamedValue>
            </om:parameter>

            <om:observedProperty  xlink:href="http://data.fmi.fi/fmi-apikey/OMA-API-AVAIN/meta?observableProperty=forecast&amp;param=geopheight&amp;language=eng"/>
	                <om:featureOfInterest>
                <sams:SF_SpatialSamplingFeature gml:id="enn-s-1-1-geopheight">
          <sam:sampledFeature>
		<target:LocationCollection gml:id="sampled-target-1-1-geopheight">
		    <target:member>
		    <target:Location gml:id="forloc-geoid-658225-pos-geopheight">
		        <gml:identifier codeSpace="http://xml.fmi.fi/namespace/stationcode/geoid">658225</gml:identifier>
			<gml:name codeSpace="http://xml.fmi.fi/namespace/locationcode/name">Helsinki</gml:name>
			<gml:name codeSpace="http://xml.fmi.fi/namespace/locationcode/geoid">658225</gml:name>
			<target:representativePoint xlink:href="#point-658225-geopheight"/>
			<target:country codeSpace="http://xml.fmi.fi/namespace/location/country">Finland</target:country>
			<target:timezone>Europe/Helsinki</target:timezone>
			<target:region codeSpace="http://xml.fmi.fi/namespace/location/region">Helsinki</target:region>
		    </target:Location></target:member>
		</target:LocationCollection>
 	   </sam:sampledFeature>
                <sams:shape>
                    <gml:MultiPoint gml:id="foi-multipoint-1-1-geopheight">
                        <gml:pointMembers>
                           <gml:Point gml:id="point-658225-geopheight" srsName="http://www.opengis.net/def/crs/EPSG/0/4258" srsDimension="2">
                               <gml:name>Helsinki</gml:name>
                               <gml:pos>60.16952 24.93545 </gml:pos>
                           </gml:Point>
                        </gml:pointMembers>
                    </gml:MultiPoint>
                </sams:shape>
            </sams:SF_SpatialSamplingFeature>
        </om:featureOfInterest>
                        <om:result>
                    <wml2:MeasurementTimeseries gml:id="mts-1-1-geopheight">
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-19T22:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-19T23:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-20T00:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-20T01:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-20T02:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-20T03:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-20T04:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-20T05:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-20T06:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-20T07:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-20T08:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-20T09:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-20T10:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-20T11:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-20T12:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-20T13:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-20T14:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-20T15:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-20T16:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-20T17:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-20T18:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-20T19:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-20T20:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-20T21:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-20T22:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-20T23:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-21T00:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-21T01:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-21T02:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-21T03:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-21T04:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-21T05:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-21T06:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-21T07:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-21T08:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                        <wml2:point>
                            <wml2:MeasurementTVP>
                                      <wml2:time>2014-02-21T09:00:00Z</wml2:time>
				      <wml2:value>7.03</wml2:value>
                            </wml2:MeasurementTVP>
                        </wml2:point>
                    </wml2:MeasurementTimeseries>
                </om:result>

        </omso:PointTimeSeriesObservation>
    </wfs:member>

    <!-- paljon <wfs:member>-osia lisää -->

</wfs:FeatureCollection>

The Alchemist [20.02.2014 00:51:14]

#

No ihan xml-parserillahan sitä käsitellään. Oliko sulla nyt jotain oikeaakin kysyttävää?

Antti Laaksonen [20.02.2014 00:52:52]

#

Yksi helppo tapa on muodostaa säännöllinen lauseke, joka erottelee datasta kiinnostavat osat välittämättä sen enempää XML:n rakenteesta. Esimerkiksi seuraava koodi poimii kaikki wfs:member-osan alkukohdat sekä ajat ja arvot järjestyksessä.

<?php
$data = file_get_contents("data.xml");
$osa1 = "<wfs:member>";
$osa2 = "<wml2:time>(.*?)<\/wml2:time>";
$osa3 = "<wml2:value>(.*?)<\/wml2:value>";
preg_match_all("/$osa1|$osa2|$osa3/", $data, $tulos);
for ($i = 0; $i < count($tulos[0]); $i++) {
    if ($tulos[1][$i] == "" && $tulos[2][$i] == "") {
        echo "<li> uusi mittaus alkaa";
    } else if ($tulos[1][$i] != "") {
        echo "<li> aika: {$tulos[1][$i]}";
    } else if ($tulos[2][$i] != "") {
        echo "<li> arvo: {$tulos[2][$i]}";
    }
}
?>

-tossu- [20.02.2014 16:06:00]

#

Samalla vaivalla onnistuu datan parsiminen xml-parserilla.

<?php
$dom = new DomDocument();
$dom->loadXML(file_get_contents("data.xml"));
$mittaukset = $dom->getElementsByTagNameNS("http://www.opengis.net/wfs/2.0", "member");
foreach ($mittaukset as $m) {
	echo "<li> uusi mittaus alkaa";
	$pisteet = $m->getElementsByTagNameNS("http://www.opengis.net/waterml/2.0", "point");
	foreach ($pisteet as $p) {
		echo "<li> aika: " . $p->getElementsByTagName("time")->item(0)->nodeValue;
		echo "<li> arvo: " . $p->getElementsByTagName("value")->item(0)->nodeValue;
	}
}

Antti Laaksonen [20.02.2014 17:17:59]

#

Totta, en tajunnut että se onnistuisi noin lyhyellä koodilla.

Vastaus

Aihe on jo aika vanha, joten et voi enää vastata siihen.

Tietoa sivustosta