2

I tried to build new type, but SimpleFeatureType.getCoordinateReferenceSystem() returns null

     //create polygon
    GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( );
    com.vividsolutions.jts.geom.Polygon polygonFromCoordinates = geometryFactory.createPolygon(coordinates);

    SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();

    // set the name
    b.setName("Garden");

    // add some properties
    b.add("name", String.class);

    // add a geometry property
     CoordinateReferenceSystem coordinateReferenceSystem = DefaultGeographicCRS.WGS84;

    b.setCRS(coordinateReferenceSystem); // set crs first
    b.add("location", Polygon.class); // then add geometry
    b.crs(coordinateReferenceSystem);
    // build the type
    final SimpleFeatureType FLAG = b.buildFeatureType();
    System.out.println(FLAG.getCoordinateReferenceSystem());

    System.out.println(FLAG);

imports:

import com.vividsolutions.jts.geom.*;
import gis.agne.counting.Value;
import org.geotools.data.FeatureSource;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.feature.DefaultFeatureCollection;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.Envelope2D;
import org.geotools.geometry.GeometryBuilder;
import org.geotools.geometry.jts.GeometryClipper;
import org.geotools.geometry.jts.JTS;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.geotools.styling.*;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.event.MapMouseEvent;
import org.opengis.feature.*;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.AttributeDescriptor;
import org.opengis.feature.type.GeometryDescriptor;
import org.opengis.feature.type.Name;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory2;
import org.opengis.filter.identity.FeatureId;
import org.opengis.filter.sort.SortBy;
import org.opengis.geometry.BoundingBox;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.util.ProgressListener;

import javax.sound.sampled.Line;
import javax.swing.*;
import java.awt.*;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.*;
import java.util.List;

pom.xml:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>gis.agne</groupId>
  <artifactId>initial</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>initial</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <geotools.version>20-SNAPSHOT</geotools.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.geotools</groupId>
      <artifactId>gt-shapefile</artifactId>
      <version>${geotools.version}</version>
    </dependency>
    <dependency>
      <groupId>org.geotools</groupId>
      <artifactId>gt-epsg-hsql</artifactId>
      <version>${geotools.version}</version>
    </dependency>
    <dependency>
      <groupId>org.geotools.jdbc</groupId>
      <artifactId>gt-jdbc-postgis</artifactId>
      <version>${geotools.version}</version>
    </dependency>
    <dependency>
      <groupId>org.geotools</groupId>
      <artifactId>gt-swing</artifactId>
      <version>${geotools.version}</version>
    </dependency>
  </dependencies>
  <repositories>
    <repository>
      <id>maven2-repository.dev.java.net</id>
      <name>Java.net repository</name>
      <url>http://download.java.net/maven/2</url>
    </repository>
    <repository>
      <id>osgeo</id>
      <name>Open Source Geospatial Foundation Repository</name>
      <url>http://download.osgeo.org/webdav/geotools/</url>
    </repository>
    <repository>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
      <id>boundless</id>
      <name>Boundless Maven Repository</name>
      <url>http://repo.boundlessgeo.com/main</url>
    </repository>
  </repositories>
  <build>
    <plugins>
      <plugin>
        <inherited>true</inherited>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
0

The following code (basically cut and pasted from the Q) works for me:

  public static void main(String[] args) {
    SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();

    // set the name
    b.setName("Garden");

    // add some properties
    b.add("name", String.class);

    CoordinateReferenceSystem coordinateReferenceSystem = DefaultGeographicCRS.WGS84;
    // add a geometry property
    b.setCRS(coordinateReferenceSystem); // set crs first
    b.add("location", Polygon.class); // then add geometry
    b.crs(coordinateReferenceSystem);
    // build the type
    final SimpleFeatureType FLAG = b.buildFeatureType();
    System.out.println(FLAG.getCoordinateReferenceSystem());

    System.out.println(FLAG);
  }

Giving the following output:

GEOGCS["WGS84(DD)", 
  DATUM["WGS84", 
    SPHEROID["WGS84", 6378137.0, 298.257223563]], 
  PRIMEM["Greenwich", 0.0], 
  UNIT["degree", 0.017453292519943295], 
  AXIS["Geodetic longitude", EAST], 
  AXIS["Geodetic latitude", NORTH]]
SimpleFeatureTypeImpl http://www.opengis.net/gml:Garden identified extends Feature(name:name,location:location)

In the question you don't show how you create the coordinateSystem you pass into the method so that may well be where the issue occurs.

11
  • For me it still returns null :) – Agniezhk Jun 6 '18 at 15:20
  • I get coordinate system from map.getCoordinateReferenceSystem(), map type is MapContent – Agniezhk Jun 6 '18 at 15:22
  • with DefaultGeographicCRS.WGS84 it aslo returns null – Agniezhk Jun 6 '18 at 15:23
  • what version of getools are you using? – Ian Turton Jun 6 '18 at 15:26
  • <geotools.version>20-SNAPSHOT</geotools.version> from pom.xml file – Agniezhk Jun 6 '18 at 15:41

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.