<HTML><BODY style="word-wrap: break-word; -khtml-nbsp-mode: space; -khtml-line-break: after-white-space; ">Hi Marc,<DIV><BR class="khtml-block-placeholder"></DIV><DIV>Exactly. You are perceptive- immediately after I got things working I realized that the way the Php FOR LOOP worked it would make it impossible to write out KML for records returned by a dynamically changing query.</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>As such I worked with the example here (<A href="http://code.google.com/support/bin/answer.py?answer=69906&topic=11364">http://code.google.com/support/bin/answer.py?answer=69906&topic=11364</A>) and modified the mysql functions to work for postgres and arrived essentially at the exact example you just sent. Thanks!</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>This approach is cool because it allows kml to be written for any number of rows returned by a query as long as some column in the database contains a numeric or string unique id.</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>I've posted the generalized form of the script below. There are two noteworthy additions for those interested:</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>One is the use of "askml(centroid(the_geom))" to grab the center point of each polygon, which is used in the while loop to create kml labels. </DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>The other is the use of only a single postgis query. PHP loops through the results as an array and writes each kml to the local file system. I'm currently testing the speed of this method vs. using the COPY command. If you have any opinions about speed or shortfalls of having php write to the filesystem I would be interested to hear.</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>Also, if anyone is interested in playing with the full script just let me know and I'll send it along.</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>Cheers,</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>Dane</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV><?php</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>$conn= pg_connect($cstring);</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>//Form up KML Header and Styles</DIV><DIV>$kml_header .= 'lots of XML HERE if you like styling';</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>// Select all the rows in a table that match given criteria.</DIV><DIV>$sql = "SELECT *,  askml(the_geom) as poly_geometry,  askml(centroid(the_geom)) as poly_centroid from table where = any_value";</DIV><DIV>$result =  pg_exec($sql);</DIV><DIV>while ($row = pg_fetch_array($result)) {</DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN></DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre">     </SPAN>//KML Header</DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN>$kml = $kml_header</DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre">   </SPAN></DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre">     </SPAN>//Key Variable</DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre">       </SPAN>$name = $row['id'] //where id is some unique in selected table id whether text or number</DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre">     </SPAN></DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre">     </SPAN>//Form up KML polygon</DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre">        </SPAN>$kml .= '<Placemark>';</DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN>$kml .= '<name>' . $name . '</name>';</DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre">    </SPAN>$kml .= '<styleUrl>#transbluePoly</styleUrl>';  </DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN>$kml .= $row['poly_geometry'] ;</DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre">      </SPAN>$kml .= '</Placemark>';</DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre">        </SPAN></DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre">     </SPAN>//Form up KML label at centroid of polygon</DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre">   </SPAN>$kml .= '<Placemark>';</DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN>$kml .= '<name>' . $name . '</name>';</DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre">    </SPAN>$kml .= '<styleUrl>#label_map</styleUrl>';</DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre">       </SPAN>$kml .= $row['poly_centroid'] ;</DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre">      </SPAN>$kml .= '</Placemark>';</DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre">        </SPAN></DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre">     </SPAN>//KML Footer</DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN>$kml .= '</Document></kml>';</DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre">     </SPAN></DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre">     </SPAN>//Use PHP to write each kml to the local filesystem with respective id number</DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre">        </SPAN>$filepath = '/Users/Shared/kml/' . $name . '.kml';</DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre">   </SPAN>file_put_contents("$filepath", "$kml");</DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre">  </SPAN>}</DIV><DIV><SPAN class="Apple-tab-span" style="white-space:pre">    </SPAN></DIV><DIV>?></DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV><BR><DIV><DIV>On Nov 8, 2007, at 8:09 AM, Marc Compte wrote:</DIV><BR class="Apple-interchange-newline"><BLOCKQUOTE type="cite"><P style="margin: 0.0px 0.0px 0.0px 0.0px"><FONT face="Lucida Grande" size="3" style="font: 12.0px Lucida Grande">...</FONT></P> <P style="margin: 0.0px 0.0px 0.0px 0.0px"><FONT face="Lucida Grande" size="3" style="font: 12.0px Lucida Grande">$conn= pg_connect($cstring);</FONT></P> <P style="margin: 0.0px 0.0px 0.0px 0.0px"><FONT face="Lucida Grande" size="3" style="font: 12.0px Lucida Grande">$sql="SELECT Id FROM your_table";</FONT></P> <P style="margin: 0.0px 0.0px 0.0px 0.0px"><FONT face="Lucida Grande" size="3" style="font: 12.0px Lucida Grande">$rs=pg_exec($sql);</FONT></P> <P style="margin: 0.0px 0.0px 0.0px 0.0px"><FONT face="Lucida Grande" size="3" style="font: 12.0px Lucida Grande">while ($row=pg_fetch_array($rs)) {</FONT></P> <P style="margin: 0.0px 0.0px 0.0px 0.0px"><FONT face="Lucida Grande" size="3" style="font: 12.0px Lucida Grande">$query = pg_query("Copy (Select '$header' || askml(the_geom) || '$footer' From YOUR_TABLE Where id=".$row["Id"].") TO 'DATA_PATH/record_".$row["Id"].".kml'; ");</FONT></P> <P style="margin: 0.0px 0.0px 0.0px 0.0px"><FONT face="Lucida Grande" size="3" style="font: 12.0px Lucida Grande">}</FONT></P> </BLOCKQUOTE></DIV><BR></DIV></BODY></HTML>