GeoserverCommand.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace GeoserverBundle\Command;
  3. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  4. use Symfony\Component\Console\Input\InputInterface;
  5. use Symfony\Component\Console\Input\InputOption;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. use Symfony\Component\Process\Process;
  8. use Symfony\Component\Process\Exception\ProcessFailedException;
  9. class GeoserverCommand extends ContainerAwareCommand
  10. {
  11. protected function configure()
  12. {
  13. $this
  14. ->setName('geoserver:test')
  15. ->setDescription('Geoserver test connection data')
  16. ->setHelp('This command allows test geoserver')
  17. ->setDefinition(array(
  18. ))
  19. ;
  20. }
  21. /**
  22. * @param InputInterface $input
  23. * @param OutputInterface $output
  24. */
  25. protected function execute(InputInterface $input, OutputInterface $output)
  26. {
  27. $this->output = $output;
  28. if(!$this->getContainer()->getParameter('geoserver_service')) {
  29. $output->writeln(sprintf('<error>Geoserver Service false</error>',array()));
  30. return;
  31. }
  32. $path = $this->getContainer()->getParameter('geoserver_path_shapes');
  33. $host = $this->getContainer()->getParameter('geoserver_host');
  34. $port = $this->getContainer()->getParameter('geoserver_port');
  35. $user = $this->getContainer()->getParameter('geoserver_user');
  36. $pass = $this->getContainer()->getParameter('geoserver_pass');
  37. $this->output->writeln("<question>## CONFIG ##</question>");
  38. $this->output->writeln("geoserver_path_shapes: <info>$path</info>");
  39. $this->output->writeln("geoserver_host: <info>$host</info>");
  40. $this->output->writeln("geoserver_port: <info>$port</info>");
  41. $this->output->writeln("geoserver_user: <info>$user</info>");
  42. $this->output->writeln("geoserver_pass: <info>$pass</info>");
  43. $geoserver = $this->getContainer()->get('geoserver.api');
  44. $rest = $geoserver->getUrlRest();
  45. $this->output->writeln("");
  46. $this->output->writeln("<question>## URLS ##</question>");
  47. $this->output->writeln("REST: <info>$rest</info>");
  48. foreach(array("onuRxPower","onuRxPowerLabel","onuTxPower","onuTxPowerLabel","onuStatus","ponRxPowerLabel") as $k => $style) {
  49. $data = $geoserver->existResource($rest."/styles/{$style}.json");
  50. if($data) {
  51. $this->output->writeln("Style $style: <info>OK</info>");
  52. } else {
  53. $this->output->writeln("## Style $style: <error>Not found</error> ##");
  54. }
  55. }
  56. $file_name = "onu_stats_demo";
  57. $dir = "$path/demo";
  58. $this->output->writeln("");
  59. $this->output->writeln("<question>## TEST SERVICE ##</question>");
  60. if(!is_dir($dir)) {
  61. mkdir($dir, 0777, true);
  62. chown($dir, "www-data");
  63. chmod($dir, 0777);
  64. }
  65. $file = dirname(dirname(__FILE__))."/Resources/stats_demo.json";
  66. if(file_exists($file)) {
  67. copy($file, $dir."/".$file_name.".json");
  68. }
  69. $process = new Process("ogr2ogr -f 'ESRI Shapefile' {$dir}/{$file_name}.shp {$dir}/{$file_name}.json");
  70. $process->run();
  71. if (!$process->isSuccessful()) {throw new ProcessFailedException($process);}
  72. echo $process->getOutput();
  73. $this->output->writeln("Demo $dir");
  74. foreach(array("dbf","json","prj","shp","shx") as $k => $ext) {
  75. if(file_exists($dir."/".$file_name.".".$ext)) {
  76. chown($dir."/".$file_name.".".$ext, "www-data");
  77. $this->output->writeln("File {$file_name}.{$ext}: <info>OK</info>");
  78. } else {
  79. $this->output->writeln("File {$file_name}.{$ext}: <error>Not found</error>");
  80. }
  81. }
  82. $workspace = "demo";
  83. $this->output->writeln("<info>Create workspace demo</info>");
  84. $geoserver->createWorkspace($workspace);
  85. $this->output->writeln("<info>Update shapes in demo</info>");
  86. $geoserver->updateShape($workspace);
  87. $styles = array();
  88. foreach(array("onuRxPower","onuRxPowerLabel","onuTxPower","onuTxPowerLabel","onuStatus") as $k => $style)
  89. {
  90. $styles[] = "<style><name>{$style}</name><atom:link rel='alternate' href='{$rest}/styles/{$style}.xml' type='application/xml'/></style>";
  91. }
  92. $stylesXml = "<layer><styles class='linked-hash-set'>".implode("",$styles)."</styles></layer>";
  93. $rest = "layers/{$file_name}.xml";
  94. $this->output->writeln("<info>Set styles on shape demo</info>");
  95. $geoserver->putData($rest, $stylesXml);
  96. $this->output->writeln("");
  97. $url = $geoserver->getUrlWms("demo")."?service=WMS&version=1.1.0&request=GetMap&layers=demo:onu_stats_demo&styles=&bbox=-61.7008896,-33.1212889,-61.21277504,-32.01828173&width=339&height=768&srs=EPSG:4326&format=application/openlayers";
  98. $this->output->writeln("<question>## $url ##</question>");
  99. $this->output->writeln("<info>Fin</info>");
  100. // curl -v -u admin:geoserver -XPUT -H "Content-type: text/plain" -d "file:/var/www/shapes/demo/" "http://geoserver.fd3.flowdat.com/geoserver/rest/workspaces/demo/datastores/shapefiles/external.shp?configure=all"
  101. }
  102. }