Skip to content

Spatial Data Create/Update API

Backendless SDK includes special classes which facilitate saving of spatial data values in the database. The same classes are used to represent spatial data when it is retrieved from the database. For more information about data retrieval, see the Spatial Data Retrieval API section.

POINT Values

Consider the following table schema in Backendless database. Notice the pickupLocation and the dropoffLocation columns. Both are of type POINT:

sample-schema-geo

The following code demonstrates how to create a new object with POINT properties in the Order table:

Dictionary<String, Object> order = new Dictionary<String, Object>();
Order[ "orderName" ] = "Fun times";
Order[ "pickUpLocation" ] = new Point().SetLatitude( 55.782309 ).SetLongitude( 37.578639 );
order[ "dropoffLocation" ] = new Point().SetLatitude( 55.752917 ).SetLongitude( 37.618900 );
Backendless.Data.Of( "Order" ).Save( order, new AsyncCallback<Dictionary<String, Object>>(
savedOrder=>
{
  String objectId = (String) savedOrder[ "objectId" ];
},
fault=>
{
  Console.WriteLine( fault.ToString() );
}));

Order class:

public class Order
{
  public String ObjectId {get;set;}
  public Point PickupLocation {get;set;}
  public Point DropoffLocation {get;set;}
  public String OrderName {get;set;}
}
Code to create and save an order in the database:
Order myOrder = new Order();
myOrder.OrderName = "Fun times";
myOrder.PickupLocation = new Point().SetX( 55.782309 ).SetY( 37.578639 );
myOrder.DropoffLocation = new Point().SetX( 55.752917 ).SetY( 37.618900 );

Backendless.Data.Of<Order>().Save( myOrder, new AsyncCallback<Order>(
  savedOrder=>
  {
    String objectId = savedOrder.ObjectId;
  },
  fault=>
  {
    Console.WriteLine( fault.ToString());
  }));

Codeless Reference

The example below creates a new object containing two GeoJSON (Point) objects and saves it to the database called Order:

data_spatial_createupdate_1

where:

Argument                Description
table name Name of the data table where a new record must be saved.
object An object to save in the database. Object properties must match the names of the table columns. The object must not have the objectId property.
return result Optional parameter. When this box is checked, the operation returns the saved object with the objectId property.

When you run the code(or Codeless logic), you will see the following in the database:

sample-geo-create-map

LINESTRING Values

Creating objects with LINESTRING properties works similarly to the example shown above - your code needs to use corresponding class from the SDK. For example, the code below creates an object in the Travel table with a LINESTRING route:

String route66LineString = "LINESTRING (-87.52683788 41.85716752, " +
        "-90.13875858 38.68967135," +
        " -95.93953983 36.2131248, " +
        "-97.49959842 35.53656483, " +
        "-101.8282117 35.26791494, " +
        "-105.87118045 35.72083154, " +
        "-106.61825076 35.14794417, " +
        "-111.63900272 35.20182535, " +
        "-118.24178592 34.07195769)";
LineString path = LineString.FromWKT<LineString>( route66LineString );
Dictionary<String, Object> travelObject = new Dictionary<String, Object>();
travelObject[ "name" ] = "Route 66";
travelObject[ "route" ] = path;
Backendless.Data.Of( "Travel" ).Save( travelObject, new AsyncCallback<Dictionary<String, Object>>(
savedObject=>
{
  String objectId = (String) savedObject[ "objectId" ];
},
fault=>
{
  Console.WriteLine( fault.ToString() );
}));

Travel class:

public class Travel
{
  public String Name {get;set;}
  public LineString Route {get;set;}
  public String ObjectId {get;set;}
}
Code to create and save an object in the database:
String route66LineString = "LINESTRING (-87.52683788 41.85716752, " +
 "-90.13875858 38.68967135," +
 " -95.93953983 36.2131248, " +
 "-97.49959842 35.53656483, " +
 "-101.8282117 35.26791494, " +
 "-105.87118045 35.72083154, " +
 "-106.61825076 35.14794417, " +
 "-111.63900272 35.20182535, " +
 "-118.24178592 34.07195769)"

LineString path = LineString.FromWKT( route66LineString );
Travel travelObject = new Travel();
travelObject.Name = "Route 66";
travelObject.Route = path;

Backendless.Data.Of<Travel>().Save( travelObject, new AsyncCallback<Travel>(
  savedObject=>
  {
    String objectId = savedObject.ObjectId;
  },
  fault=>
  {

  }));

Notice the example above uses the LineString.fromWKT method to convert a string representation of a geometry into an instance of the LineString class. Alternatively, the code can use a constructor in the LineString class which takes an collection of Point objects. The example below and the one above produce exactly the same result using different approaches available in the SDK:

Dictionary<String, Object> travelObject = new Dictionary<String, object>();
travelObject[ "name" ] = "Route 66";

List<Point> points = new List<Point>();
points.Add( new Point().SetLongitude( -90.13875858 ).SetLatitude( 38.68967135 ) );
points.Add( new Point().SetLongitude( -95.93953983 ).SetLatitude( 36.2131248 ) );
points.Add( new Point().SetLongitude( -97.49959842 ).SetLatitude( 35.53656483 ) );
points.Add( new Point().SetLongitude( -101.8282117 ).SetLatitude( 35.26791494 ) );
points.Add( new Point().SetLongitude( -105.87118045 ).SetLatitude( 35.72083154 ) );
points.Add( new Point().SetLongitude( -106.61825076 ).SetLatitude( 35.14794417 ) );
points.Add( new Point().SetLongitude( -111.63900272 ).SetLatitude( 35.20182535 ) );
points.Add( new Point().SetLongitude( -118.24178592 ).SetLatitude( 34.07195769 ) );

travelObject[ "route" ] = new LineString( points ) );
Backendless.Data.Of( "Travel" ).Save( travelObject, new AsyncCallback<Dictionary<String, Object>>(
savedObject=>
{
  String objectId = (String) savedObject[ "objectId" ];
},
fault=>
{
  Console.WriteLine( fault.ToString() );
}));

Travel class:

public class Travel
{
  public String Name {get;set;}
  public LineString Route {get;set;}
  public String ObjectId {get;set;}
}
Code to create and save an object in the database:
Travel travelObject = new Travel();
travelObject.Name = "Route 66";

List<Point> points = new ArrayList<>();
points.Add( new Point().SetLongitude( -90.13875858 ).SetLatitude( 38.68967135 ) );
points.Add( new Point().SetLongitude( -95.93953983 ).SetLatitude( 36.2131248 ) );
points.Add( new Point().SetLongitude( -97.49959842 ).SetLatitude( 35.53656483 ) );
points.Add( new Point().SetLongitude( -101.8282117 ).SetLatitude( 35.26791494 ) );
points.Add( new Point().SetLongitude( -105.87118045 ).SetLatitude( 35.72083154 ) );
points.Add( new Point().SetLongitude( -106.61825076 ).SetLatitude( 35.14794417 ) );
points.Add( new Point().SetLongitude( -111.63900272 ).SetLatitude( 35.20182535 ) );
points.Add( new Point().SetLongitude( -118.24178592 ).SetLatitude( 34.07195769 ) );

travelObject.Route = new LineString( points );
Backendless.Data.Of<Travel>().Save( travelObject, new AsyncCallback<Travel>(
  savedObject=>
  {
    String objectId = savedObject.ObjectId;
  },
  fault=>
  {
    Console.WriteLine( fault.ToString() );
  }));

Codeless Reference

The example below creates a new object containing one GeoJSON (LineString) object and saves it to the database called Travel:

data_spatial_createupdate_2

where:

Argument                Description
table name Name of the data table where a new record must be saved.
object An object to save in the database. Object properties must match the names of the table columns. The object must not have the objectId property.
return result Optional parameter. When this box is checked, the operation returns the saved object with the objectId property.

When you run the code(or Codeless Logic), you will see the following object in the database:

sample-linestring

POLYGON Values

Creating objects with properties of type POLYGON can be done by using the BackendlessAPI.Persistence.Polygon class. Consider the following example, it is a data table called Building with a schema which includes a column named shape of type POLYGON:

building-table-schema

The code below demonstrates how to create a new object with a POLYGON property in the database:

Polygon pentagonBuildingShape = Polygon.FromWKT<Polygon>( pentagonPolygonString );
Dictionary<String, Object> buildingObject = new Dictionary<String, object>();
buildingObject[ "name" ] = "Pentagon";
buildingObject[ "shape" ] = pentagonBuildingShape;
Backendless.Data.Of( "Building" ).Save( buildingObject, new AsyncCallback<Dictionary<String, Object>>(
savedObject=>
{
  String objectId = (String) savedObject[ "objectId" ];
},
fault=>
{
  Console.WriteLine( fault.ToString() );
}));

Building class:

public class Building
{
  public String Name {get;set;}
  public Polygon Shape {get;set;}
  public String ObjectId {get;set;}
}
Code to create and save an object in the database:
String pentagonPolygonString = "POLYGON (" +
        "(-77.05786152 38.87261877, " +
        "-77.0546978 38.87296123, " +
        "-77.05317431 38.87061405, " +
        "-77.0555883 38.86882611, " +
        "-77.05847435 38.87002898, " +
        "-77.05786152 38.87261877), " +
        "(-77.05579215 38.87026286, " +
        "-77.05491238 38.87087264, " +
        "-77.05544882 38.87170794, " +
        "-77.05669337 38.87156594, " +
        "-77.05684357 38.87072228, " +
        "-77.05579215 38.87026286))";
Polygon pentagonBuildingShape = Polygon.FromWKT( pentagonBuildingShape);

Building buildingObject = new Building();
buildingObject.Name = "Pentagon";
buildingObject.Shape = pentagonBuildingShape;

Backendless.Data.Of<Building>().Save( buildingObject, new AsyncCallback<Building>(
  savedBuilding=>
  {
    String objectId = savedBuilding.ObjectId;
  },
  fault=>
  {
    Console.WriteLine(fault.ToString());
  }));

Notice the example above uses the Polygon.fromWKT method to convert a string representation of a geometry into an instance of the Polygon class. Alternatively, the code can use a constructor in the Polygon class which accepts two collections - the boundary points and the holes. The example below and the one above produce exactly the same result using different approaches available in the SDK:

List<Point> boundary = new List<Point>();
boundary.Add( new Point().SetLongitude( -77.05786152 ).SetLatitude( 38.87261877 ) );
boundary.Add( new Point().SetLongitude( -77.0546978 ).SetLatitude( 38.87296123 ) );
boundary.Add( new Point().SetLongitude( -77.05317431 ).SetLatitude( 38.87061405 ) );
boundary.Add( new Point().SetLongitude( -77.0555883 ).SetLatitude( 38.86882611 ) );
boundary.Add( new Point().SetLongitude( -77.05847435 ).SetLatitude( 38.87002898 ) );
boundary.Add( new Point().SetLongitude( -77.05786152 ).SetLatitude( 38.87261877 ) );

List<Point> hole = new List<Point>();
hole.Add( new Point().SetLongitude( -77.05579215 ).SetLatitude( 38.87026286 ) );
hole.Add( new Point().SetLongitude( -77.05491238 ).SetLatitude( 38.87087264 ) );
hole.Add( new Point().SetLongitude( -77.05544882 ).SetLatitude( 38.87170794 ) );
hole.Add( new Point().SetLongitude( -77.05669337 ).SetLatitude( 38.87156594 ) );
hole.Add( new Point().SetLongitude( -77.05684357 ).SetLatitude( 38.87072228 ) );
hole.Add( new Point().SetLongitude( -77.05579215 ).SetLatitude( 38.87026286 ) );
List<LineString> holes = new List<LineString>();
holes.Add( new LineString( hole ) );

Polygon pentagonBuildingShape = new Polygon( boundary, holes );

Dictionary<String, Object> buildingObject = new Dictionary<String, Object>();
buildingObject[ "name" ] = "Pentagon";
buildingObject[ "shape" ] = pentagonBuildingShape;
Backendless.Data.Of( "Building" ).Save( buildingObject, new AsyncCallback<Dictionary<String, Object>>(
savedBuilding=>
{
  String objectId = (String) savedBuilding[ "objectId" ];
},
fault=>
{
  Console.WriteLine( fault.ToString() );
}));

Building class:

using BackendlessAPI;

public class Building
{
  public String Name {get;set;}
  public Polygon Shape {get;set;}
  public String ObjectId {get;set;}
}
Code to create and save an object in the database:
List<Point> boundary = new List<Point>();
boundary.Add( new Point().SetLongitude( -77.05786152 ).SetLatitude( 38.87261877 ));
boundary.Add( new Point().SetLongitude( -77.0546978 ).SetLatitude( 38.87296123 ));
boundary.Add( new Point().SetLongitude( -77.05317431 ).SetLatitude( 38.87061405 ));
boundary.Add( new Point().SetLongitude( -77.0555883 ).SetLatitude( 38.86882611 ));
boundary.Add( new Point().SetLongitude( -77.05847435 ).SetLatitude( 38.87002898 ));
boundary.Add( new Point().SetLongitude( -77.05786152 ).SetLatitude( 38.87261877 ));

List<Point> hole = new List<Point>();
hole.Add( new Point().SetLongitude( -77.05579215 ).SetLatitude( 38.87026286 ));
hole.Add( new Point().SetLongitude( -77.05491238 ).SetLatitude( 38.87087264 ));
hole.Add( new Point().SetLongitude( -77.05544882 ).SetLatitude( 38.87170794 ));
hole.Add( new Point().SetLongitude( -77.05669337 ).SetLatitude( 38.87156594 ));
hole.Add( new Point().SetLongitude( -77.05684357 ).SetLatitude( 38.87072228 ));
hole.Add( new Point().SetLongitude( -77.05579215 ).SetLatitude( 38.87026286 ));
List<LineString> holes = new List<LineString>();
holes.Add( new LineString( hole ) );

Polygon pentagonBuildingShape = new Polygon( boundary, holes )
Building buildingObject = new Building();
buildingObject.Name = "Pentagon";
buildingObject.Shape = pentagonBuildingShape;

Backendless.Data.Of<Building>().Save( buildingObject, new AsyncCallback<Building>(
  savedBuilding=>
  {
    String objectId = savedBuilding.ObjectId;
  },
  fault=>
  {
    Console.WriteLine( fault.ToString() );
  }));

Codeless Reference

The example below creates a new object containing one GeoJSON (Polygon) object and saves it to the database called geolocation:

data_spatial_types_13

where:

Argument                Description
table name Name of the data table where a new record must be saved.
object An object to save in the database. Object properties must match the names of the table columns. The object must not have the objectId property.
return result Optional parameter. When this box is checked, the operation returns the saved object with the objectId property.

After running the code(or Codeless Logic), you will see the following object in the database:

data_spatial_types_14