Dynamically load and display shapefiles from PostGIS 2.0 into Mapguide using .NET

by user15083   Last Updated December 19, 2018 13:22 PM

I have a C# mechanism for Loading shapefiles into PostreSQL/PostGIS now I would like to dynamically load it on Mapguide fusion template.In either C# or VB.NET code behind.

Use case is: 1.use uploads a zip folder with shapefile 2. .NET code behind checks for certain attributes then uploads it to PostgreSQL/PostGIS Database 3. upon successful upload, display the layer into Mapguide Fusion template 4.Overlay it over base layers

Answers 2

Not sure how familiar you are with the MapGuide Web API, but that's what you'll have to use to achieve this.

Basically, you'll have to dynamically generate a Layer Definition XML. You can see the XML of your existing layers for a reference or even load a generic one and modify the source of the data to the newly loaded data that the user uploaded... here's an example (PHP):

  // some initialization
  $user = new MgUserInformation([SESSION_ID]);
  $site = new MgSiteConnection();

  // get the services
  $res = $site->CreateService(MgServiceType::ResourceService);

  // get the map object
  $map = new MgMap($site);

  // get the XML content of a generic layer, returns MgByteReader
  $layer_definition = $res->GetResourceContent(new MgResourceIdentifier('Library://Layers/generic.LayerDefinition'));
  $dom = new DOMDocument();

  // Modify where the data source is
  $node = $dom->getElementsByTagName('FeatureName')->item(0);
  $node->nodeValue = "schema.new_shp_table";
  $new_def = $dom->saveXML();

  // create and save the new layer to the current session only
  $bs = new MgByteSource($new_def, strlen($new_def));
  $new_resource_id = new MgResourceIdentifier("Session:[SESSION_ID]//new_shp_layer.LayerDefinition");
  $res->SetResource($new_resource_id, $bs->GetReader(), null);

  $new_layer = new MgLayer($new_resource_id, $res);
  $new_layer->SetLegendLabel('New Uploaded Data');

  $layers->Insert(1, $new_layer);


Where [MAPNAME] and [SESSION_ID] are passed into the script. This is all assuming you want the new data to be only available to the current session map. Otherwise you'll have to be able to SAVE the new layer into the MapDefinition on the server.

Hope this helps!

February 12, 2013 08:18 AM

@user15083 Have you achieve this task? Upload shape file in zip format(like Redline)

December 19, 2018 13:12 PM

Related Questions

Updated July 09, 2018 12:22 PM

Updated July 12, 2017 09:22 AM

Updated February 23, 2018 10:22 AM

Updated August 24, 2015 19:09 PM