Skip to content

Instantly share code, notes, and snippets.

@jumpinjackie
Created September 22, 2016 14:14
Show Gist options
  • Save jumpinjackie/35b0e051e427310524fd20b210adfea4 to your computer and use it in GitHub Desktop.
Save jumpinjackie/35b0e051e427310524fd20b210adfea4 to your computer and use it in GitHub Desktop.
Inserting/Updating/Deleting features in MapGuide
/**
* All code fragments here listed for informational purposes. Do not expect to copy/paste this code and expect it to compile
*
* All examples here assume we work against a point feature source at Library://Test.FeatureSource with a feature class structured like so:
*
* Schema: Default
* Class: Points
* Properties:
* ID: int (identity)
* Name: string
* Geometry: Geometry (point)
*/
/**
* You must reference the following assemblies from your wwwroot/mapviewernet/bin directory, all the other
* dlls in that directory must also exist in your application's output directory
*
* - OSGeo.MapGuide.Foundation
* - OSGeo.MapGuide.Geometry
* - OSGeo.MapGuide.PlatformBase
* - OSGeo.MapGuide.MapGuideCommon
* - OSGeo.MapGuide.Web
*
* If you use the MapGuide .net API NuGet package, disregard the above instructions as this will be automatically
* set up for the project you install the package into.
*/
using OSGeo.MapGuide;
//================================================================================
// Example 1: Inserting a point feature (using UpdateFeatures API)
//================================================================================
MgResourceIdentifier fsId = new MgResourceIdentifier("Library://Test.FeatureSource");
MgWktReaderWriter wktRw = new MgWktReaderWriter();
MgAgfReaderWriter agfRw = new MgAgfReaderWriter();
MgGeometry geom = wktRw.Read("POINT(1 1)");
MgByteReader agf = agfRw.Write(geom);
//Prepare insert values
MgPropertyCollection insertVals = new MgPropertyCollection();
MgStringProperty nameVal = new MgStringProperty("Name", "Hello World");
MgGeometryProperty geomVal = new MgGeometryProeprty("Geometry", agf);
insertVals.Add(nameVal);
insertVals.Add(geomVal);
//Prepare command collection for UpdateFeatures
MgFeatureCommandCollection commands = new MgFeatureCommandCollection();
MgInsertFeatures insertCmd = new MgInsertFeatures("Default:Points", insertVals);
commands.Add(insertCmd);
//Transaction support varies from provider to provider (you have to programmatically check the
//FDO provider capabilities to determine if this can be true
bool useTransaction = false;
//featureService is a MgFeatureService
//Call API and process result (important!)
MgPropertyCollection result = featureService.UpdateFeatures(fsId, commands, useTransaction);
for (int i = 0; i < result.GetCount(); i++)
{
MgProperty prop = result.GetItem(i);
if (prop.GetPropertyType() == MgPropertyType::Feature) //Insert result
{
MgFeatureProperty fp = (MgFeatureProperty)prop;
//Must close the reader inside to prevent busy resource errors
MgFeatureReader reader = fp.GetValue();
reader.Close();
}
else if (prop.GetPropertyType() == MgPropertyType::String) //FDO error in non-transactional mode
{
MgStringProperty sp = (MgStringProperty)prop;
String fdoError = sp.GetValue();
//Handle this error
}
}
//=======================================================================================
// Example 2: Updating the name property of a point feature (ID: 1)
//=======================================================================================
MgResourceIdentifier fsId = new MgResourceIdentifier("Library://Test.FeatureSource");
//Prepare update values
MgPropertyCollection updateVals = new MgPropertyCollection();
MgStringProperty nameVal = new MgStringProperty("Name", "Hello World");
insertVals.Add(nameVal);
//Prepare command collection for UpdateFeatures
MgFeatureCommandCollection commands = new MgFeatureCommandCollection();
MgUpdateFeatures updateCmd = new MgUpdateFeatures("Default:Points", updateVals, "ID = 1");
commands.Add(updateCmd);
//Transaction support varies from provider to provider (you have to programmatically check the
//FDO provider capabilities to determine if this can be true
bool useTransaction = false;
//featureService is a MgFeatureService
//Call API and process result (important!)
MgPropertyCollection result = featureService.UpdateFeatures(fsId, commands, useTransaction);
for (int i = 0; i < result.GetCount(); i++)
{
MgProperty prop = result.GetItem(i);
if (prop.GetPropertyType() == MgPropertyType::Int32) //Update result
{
MgInt32Property ip = (MgInt32Property)prop;
int affectedCount = ip.GetValue();
}
else if (prop.GetPropertyType() == MgPropertyType::String) //FDO error in non-transactional mode
{
MgStringProperty sp = (MgStringProperty)prop;
String fdoError = sp.GetValue();
//Handle this error
}
}
//=======================================================================================
// Example 3: Deleting a point feature with ID = 1
//=======================================================================================
MgResourceIdentifier fsId = new MgResourceIdentifier("Library://Test.FeatureSource");
//Prepare command collection for UpdateFeatures
MgFeatureCommandCollection commands = new MgFeatureCommandCollection();
MgDeleteFeatures deleteCmd = new MgDeleteFeatures("Default:Points", "ID = 1");
commands.Add(deleteCmd);
//Transaction support varies from provider to provider (you have to programmatically check the
//FDO provider capabilities to determine if this can be true
bool useTransaction = false;
//featureService is a MgFeatureService
//Call API and process result (important!)
MgPropertyCollection result = featureService.UpdateFeatures(fsId, commands, useTransaction);
for (int i = 0; i < result.GetCount(); i++)
{
MgProperty prop = result.GetItem(i);
if (prop.GetPropertyType() == MgPropertyType::Int32) //Delete result
{
MgInt32Property ip = (MgInt32Property)prop;
int deletedCount = ip.GetValue();
}
else if (prop.GetPropertyType() == MgPropertyType::String) //FDO error in non-transactional mode
{
MgStringProperty sp = (MgStringProperty)prop;
String fdoError = sp.GetValue();
//Handle this error
}
}
/**
* All code fragments here listed for informational purposes. Do not expect to copy/paste this code and expect it to compile
*
* All examples here assume we work against a point feature source at Library://Test.FeatureSource with a feature class structured like so:
*
* Schema: Default
* Class: Points
* Properties:
* ID: int (identity)
* Name: string
* Geometry: Geometry (point)
*/
/**
* Make sure MapGuideApi.jar is in your classpath and all sibling dlls are in your
* application's output directory
*
* If you use the enhanced Java API (MapGuideApiEx.jar), make sure that jar is in
* your class path instead and change all the method calls below to lowerCamelCase format
*/
import org.osgeo.mapguide.*;
//================================================================================
// Example 1: Inserting a point feature (using UpdateFeatures API)
//================================================================================
MgResourceIdentifier fsId = new MgResourceIdentifier("Library://Test.FeatureSource");
MgWktReaderWriter wktRw = new MgWktReaderWriter();
MgAgfReaderWriter agfRw = new MgAgfReaderWriter();
MgGeometry geom = wktRw.Read("POINT(1 1)");
MgByteReader agf = agfRw.Write(geom);
//Prepare insert values
MgPropertyCollection insertVals = new MgPropertyCollection();
MgStringProperty nameVal = new MgStringProperty("Name", "Hello World");
MgGeometryProperty geomVal = new MgGeometryProeprty("Geometry", agf);
insertVals.Add(nameVal);
insertVals.Add(geomVal);
//Prepare command collection for UpdateFeatures
MgFeatureCommandCollection commands = new MgFeatureCommandCollection();
MgInsertFeatures insertCmd = new MgInsertFeatures("Default:Points", insertVals);
commands.Add(insertCmd);
//Transaction support varies from provider to provider (you have to programmatically check the
//FDO provider capabilities to determine if this can be true
bool useTransaction = false;
//featureService is a MgFeatureService
//Call API and process result (important!)
MgPropertyCollection result = featureService.UpdateFeatures(fsId, commands, useTransaction);
for (int i = 0; i < result.GetCount(); i++)
{
MgProperty prop = result.GetItem(i);
if (prop.GetPropertyType() == MgPropertyType::Feature) //Insert result
{
MgFeatureProperty fp = (MgFeatureProperty)prop;
//Must close the reader inside to prevent busy resource errors
MgFeatureReader reader = fp.GetValue();
reader.Close();
}
else if (prop.GetPropertyType() == MgPropertyType::String) //FDO error in non-transactional mode
{
MgStringProperty sp = (MgStringProperty)prop;
String fdoError = sp.GetValue();
//Handle this error
}
}
//=======================================================================================
// Example 2: Updating the name property of a point feature (ID: 1)
//=======================================================================================
MgResourceIdentifier fsId = new MgResourceIdentifier("Library://Test.FeatureSource");
//Prepare update values
MgPropertyCollection updateVals = new MgPropertyCollection();
MgStringProperty nameVal = new MgStringProperty("Name", "Hello World");
insertVals.Add(nameVal);
//Prepare command collection for UpdateFeatures
MgFeatureCommandCollection commands = new MgFeatureCommandCollection();
MgUpdateFeatures updateCmd = new MgUpdateFeatures("Default:Points", updateVals, "ID = 1");
commands.Add(updateCmd);
//Transaction support varies from provider to provider (you have to programmatically check the
//FDO provider capabilities to determine if this can be true
bool useTransaction = false;
//featureService is a MgFeatureService
//Call API and process result (important!)
MgPropertyCollection result = featureService.UpdateFeatures(fsId, commands, useTransaction);
for (int i = 0; i < result.GetCount(); i++)
{
MgProperty prop = result.GetItem(i);
if (prop.GetPropertyType() == MgPropertyType::Int32) //Update result
{
MgInt32Property ip = (MgInt32Property)prop;
int affectedCount = ip.GetValue();
}
else if (prop.GetPropertyType() == MgPropertyType::String) //FDO error in non-transactional mode
{
MgStringProperty sp = (MgStringProperty)prop;
String fdoError = sp.GetValue();
//Handle this error
}
}
//=======================================================================================
// Example 3: Deleting a point feature with ID = 1
//=======================================================================================
MgResourceIdentifier fsId = new MgResourceIdentifier("Library://Test.FeatureSource");
//Prepare command collection for UpdateFeatures
MgFeatureCommandCollection commands = new MgFeatureCommandCollection();
MgDeleteFeatures deleteCmd = new MgDeleteFeatures("Default:Points", "ID = 1");
commands.Add(deleteCmd);
//Transaction support varies from provider to provider (you have to programmatically check the
//FDO provider capabilities to determine if this can be true
bool useTransaction = false;
//featureService is a MgFeatureService
//Call API and process result (important!)
MgPropertyCollection result = featureService.UpdateFeatures(fsId, commands, useTransaction);
for (int i = 0; i < result.GetCount(); i++)
{
MgProperty prop = result.GetItem(i);
if (prop.GetPropertyType() == MgPropertyType::Int32) //Delete result
{
MgInt32Property ip = (MgInt32Property)prop;
int deletedCount = ip.GetValue();
}
else if (prop.GetPropertyType() == MgPropertyType::String) //FDO error in non-transactional mode
{
MgStringProperty sp = (MgStringProperty)prop;
String fdoError = sp.GetValue();
//Handle this error
}
}
/**
* All code fragments here listed for informational purposes. Do not expect to copy/paste this code and expect it to compile
*
* All examples here assume we work against a point feature source at Library://Test.FeatureSource with a feature class structured like so:
*
* Schema: Default
* Class: Points
* Properties:
* ID: int (identity)
* Name: string
* Geometry: Geometry (point)
*/
/**
* Make sure to include constants.php, which a copy can be found in your wwwroot/mapadmin folder or wwwroot/mapviewerphp
*
* This is needed for certain constants in the MapGuide API to be defined
*/
//================================================================================
// Example 1: Inserting a point feature (using UpdateFeatures API)
//================================================================================
$fsId = new MgResourceIdentifier("Library://Test.FeatureSource");
$wktRw = new MgWktReaderWriter();
$agfRw = new MgAgfReaderWriter();
$geom = $wktRw->Read("POINT(1 1)");
$agf = $agfRw->Write($geom);
//Prepare insert values
$insertVals = new MgPropertyCollection();
$nameVal = new MgStringProperty("Name", "Hello World");
$geomVal = new MgGeometryProeprty("Geometry", $agf);
$insertVals->Add($nameVal);
$insertVals->Add($geomVal);
//Prepare command collection for UpdateFeatures
$commands = new MgFeatureCommandCollection();
$insertCmd = new MgInsertFeatures("Default:Points", $insertVals);
$commands->Add($insertCmd);
//Transaction support varies from provider to provider (you have to programmatically check the
//FDO provider capabilities to determine if this can be true
$useTransaction = false;
//featureService is a MgFeatureService
//Call API and process result (important!)
$result = $featureService->UpdateFeatures($fsId, $commands, $useTransaction);
for ($i = 0; $i < $result->GetCount(); $i++)
{
$prop = result->GetItem($i);
if ($prop->GetPropertyType() == MgPropertyType::Feature) //Insert result
{
//Must close the reader inside to prevent busy resource errors
$reader = $prop->GetValue();
$reader->Close();
}
else if ($prop->GetPropertyType() == MgPropertyType::String) //FDO error in non-transactional mode
{
$fdoError = $prop->GetValue();
//Handle this error
}
}
//=======================================================================================
// Example 2: Updating the name property of a point feature (ID: 1)
//=======================================================================================
$fsId = new MgResourceIdentifier("Library://Test.FeatureSource");
//Prepare update values
$updateVals = new MgPropertyCollection();
$nameVal = new MgStringProperty("Name", "Hello World");
$updateVals->Add($nameVal);
//Prepare command collection for UpdateFeatures
$commands = new MgFeatureCommandCollection();
$updateCmd = new MgUpdateFeatures("Default:Points", $updateVals, "ID = 1");
$commands->Add($updateCmd);
//Transaction support varies from provider to provider (you have to programmatically check the
//FDO provider capabilities to determine if this can be true
$useTransaction = false;
//featureService is a MgFeatureService
//Call API and process result (important!)
$result = $featureService->UpdateFeatures($fsId, $commands, $useTransaction);
for ($i = 0; $i < $result->GetCount(); $i++)
{
$prop = $result->GetItem($i);
if ($prop->GetPropertyType() == MgPropertyType::Int32) //Update result
{
$updatedCount = $prop->GetValue();
}
else if ($prop->GetPropertyType() == MgPropertyType::String) //FDO error in non-transactional mode
{
$fdoError = $prop->GetValue();
//Handle this error
}
}
//=======================================================================================
// Example 3: Deleting a point feature with ID = 1
//=======================================================================================
$fsId = new MgResourceIdentifier("Library://Test.FeatureSource");
//Prepare command collection for UpdateFeatures
$commands = new MgFeatureCommandCollection();
$deleteCmd = new MgDeleteFeatures("Default:Points", "ID = 1");
$commands->Add($deleteCmd);
//Transaction support varies from provider to provider (you have to programmatically check the
//FDO provider capabilities to determine if this can be true
$useTransaction = false;
//featureService is a MgFeatureService
//Call API and process result (important!)
$result = $featureService->UpdateFeatures($fsId, $commands, $useTransaction);
for ($i = 0; $i < $result->GetCount(); $i++)
{
$prop = $result->GetItem($i);
if ($prop->GetPropertyType() == MgPropertyType::Int32) //Delete result
{
$deletedCount = $prop->GetValue();
}
else if ($prop->GetPropertyType() == MgPropertyType::String) //FDO error in non-transactional mode
{
$fdoError = $prop->GetValue();
//Handle this error
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment