[postgis] Re:shp2pgsql

Hisaji ONO ono96 at fa2.so-net.ne.jp
Thu Sep 27 05:27:12 PDT 2001


Hi,

 I have created following quick and dirty two php scripts(svgmap.php,
wkt2svg.php) for creating SVG instances from WKT(Point & Polygon) which
are returned by PostGIS as a result of spatial queries.

 You must install SVG Adobe Viewer Plug-In (http://www.adobe.com/svg)
Ver 2.0 or later into your PC if it not installed..

 And change your DB info(host, ID etc.) and table info for your data.

 Sorry, these scripts are incomplete for dealing with inner polygons and
multi-polygons.

 
 List1  svgmap.php

<?php

include("wkt2svg.inc"); # load wkt2svg

//////////////////////////////////////////////////
//   Main
//////////////////////////////////////////////////

showHeader();

//
// Set DB Connection Info
//
$hostName = "your host name";
$USR = "your-id";
$PASSWRD = "your password";
$TABLE="your database name";

$dbcon = pg_connect("host=".$hostName." user=".$USR ."
password=".$PASSWRD." dbname=".$TABLE); # connect to DB
;

if (!$dbcon) { die("<hr>pg_connect </hr>");}

$sql= "SET ENABLE_SEQSCAN = OFF";

$execid = pg_exec ($dbcon, $sql);

$layerName = "your table name"; # Polygon Geometry included

$sql= "SELECT extent(GEO_VALUE) FROM ".$layerName; # get Map Extent

$execid = pg_exec ($dbcon, $sql);

if (!$execid){
} else {

 $resultstr = pg_result($execid,0,0);

 $extent = getExtent($resultstr);

// create extent
  createExtent($extent);

//
// Polygon Layer
//
  $layerName="your table name";# Polygon Geometry included
  $fillColor = "cyan"; # filled Color name or hex
  $outlineColot = "blue"; # out line # Polygon Geometry included
  $where = "geoid >= 0"; # condition

  getPolygons($dbcon, $layerName, $fillColor, $outlineColot,$where);

//
// LineString Layer
//
  $layerName="eki";# Point Geometry included
  getPoints($dbcon, $layerName); 

 // close SVG
  createFooter();

}
?>
  
------------------------------------------------------------------------
---------------------------------------

List 2 wkt2svg

<?
  //XML header
function showHeader(){
  header("Content-type: image/svg-xml");

//Header

  print('<?xml version="1.0" encoding="iso-8859-1"?>' . "\n");

// 
/*
print('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG //EN" ');

print('"http://www.w3.org/TR/2001/ PR-SVG-20010719/DTD/' .

'svg10.dtd">' . "\n");
*/
};

//generate a tag for extent
function createExtent($extent){

 $xmin = $extent[0];
 $ymin = $extent[1];

 $width= $extent[3] - $extent[0];
 $height= $extent[4] - $extent[1];

 $height2= $extent[4] + $extent[1];

  //print '<svg width="512" height="512" viewBox="'.$xmin.' '.$ymin.'
'.$width.' '.$height.'">';
  print '<svg viewBox="'.$xmin.' '.$ymin.' '.$width.' '.$height.'">';
  print '<g transform="matrix(1 0 0 -1 0 '.$height2.')" >'; #Affine
 
};
//close svg
function createFooter(){
  print '</g>';
  print('</svg>' . "\n");
};
//get extent
function getExtent($result){
 $tmpstr = str_replace("BOX3D(", "", $result);
 $tmpstr = str_replace(")", "", $tmpstr);
 $tmpstr = str_replace(",", " ", $tmpstr);
 return explode(" ", $tmpstr);
};
//start a layer or g group
function layerHeader(){
   print('<g>' . "\n");
}
//close a layer
function layerFooter(){
   print('</g>' . "\n");
}
//get coordinates of points
function getPoints($dbcon, $layerName){
  $sql= "SELECT geoid, x(GEO_VALUE), y(GEO_VALUE) FROM ".$layerName;
  $execid = pg_exec ($dbcon, $sql);

  print('<g id="'.$layerName.'">' . "\n");

  for ($i=0;$i < pg_numrows($execid);$i++){
    $id = pg_result($execid,$i,0); //id
    $x= pg_result($execid,$i,1);   // X 
    $y = pg_result($execid,$i,2); // Y

    print '<circle style="fill:red;fill-stroke:blue;" id="'.$id.'"
cx="'.$x.'" cy="'.$y.'" r="100px"/>';
  };

  print('</g>');
};
//
// create a Path for a Polygon
//
function getPolygons($dbcon, $layerName, $fillColor,$outlineColor,
$where){

 if($where == "")
   $sql= "SELECT exteriorring(GEO_VALUE) FROM ".$layerName;
 else
   $sql= "SELECT exteriorring(GEO_VALUE) FROM ".$layerName." WHERE
".$where;

 $execid = pg_exec ($dbcon, $sql);

 print('<g id="'.$layerName.'">' . "\n");

 for ($i=0;$i < pg_numrows($execid);$i++){
   $exterior = pg_result($execid,$i,0);
   //remove SRID=-1;
   $tempstr = explode(";", $exterior);
   $exterior = $tempstr[1];

   print '<path
style="fill:'.$fillColor.';stroke:'.$outlineColor.';stroke-width:22"
d="'.createPolygonPath($exterior).'" />';
 };

 print('</g>');
};
//
// generate a path
//

function createPolygonPath($result){
  $tmpstr = str_replace("LINESTRING(", "", $result);
  $tmpstr = str_replace(")", "", $tmpstr);
  $tmpstr = str_replace(",", "L", $tmpstr);
  $tmpstr = str_replace(" ", ",", $tmpstr);
 return "M".$tmpstr."z";
};

?>



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20010927/f8fb7bcd/attachment.html>


More information about the postgis-users mailing list