0

I successfully created workspace in GeoServer using cURL in PHP following the GeoServer REST Guide. But I am having problems when trying to upload Shapefile. I followed the instructions on Uploading Shapefiles from GeoServer REST Guide but I can't seem to get it right. I keep getting Wrong Magic Number error. Also I want to upload shp file rather than zip.

I am a beginner in GIS.

Update

Everything works fine when I use CURL

curl -v -u admin:geoserver -XPUT -H "Content-type: application/zip" --data-binary @trash.zip localhost:8080/geoserver/rest/workspaces/mySpace/datastores/‌​…

but fails with the following PHP code:

<?php $ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://localhost:9090/geoserver/rest/workspaces/mySpace/dat‌astores/trash/file.s‌​hp"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$post = array( "file" => "@" .realpath("trash.zip") ); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); 
curl_setopt($ch, CURLOPT_USERPWD, "admin" . ":" . "geoserver"); 
$headers = array(); 
$headers[] = "Content-Type: application/zip"; 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
$result = curl_exec($ch); 
curl_close ($ch); ?>
2

A shapefile (despite it's name) consists of between 3 and 9 files sharing a common prefix but with different suffixes. You need to zip up all of the files to get a successful upload.

  • Thank you for the response. I did zip the files and did this... curl -v -u admin:geoserver -XPUT -H "Content-type: application/zip" --data-binary @trash.zip localhost:8080/geoserver/rest/workspaces/mySpace/datastores/… This runs perfectly when done in Command line tool but does not work when is converted to php. – A. Bhujel Oct 12 '17 at 9:28
  • then you need to add the php code you are using – Ian Turton Oct 12 '17 at 10:20
  • Oh. Sorry! <?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://localhost:9090/geoserver/rest/workspaces/mySpace/datastores/trash/file.shp"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $post = array( "file" => "@" .realpath("trash.zip") ); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_USERPWD, "admin" . ":" . "geoserver"); $headers = array(); $headers[] = "Content-Type: application/zip"; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); curl_close ($ch); ?> – A. Bhujel Oct 12 '17 at 10:40
0

I was finally able to upload the shapefile to GeoServer. I followed this article by Okello Nelson. Very well explained and working article.

Thank you!

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

  • I found the answer for my question in an article which i have linked in the answer. The article has explained everything in detail. That is the reason I linked the article. – A. Bhujel Oct 17 '17 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.