use of org.locationtech.spatial4j.io.SupportedFormats in project lucene-solr by apache.
the class TestGeoJSONResponseWriter method testTransformToAllFormats.
@Test
public void testTransformToAllFormats() throws Exception {
String wkt = "POINT( 1 2 )";
SupportedFormats fmts = SpatialContext.GEO.getFormats();
Shape shape = fmts.read(wkt);
String[] check = new String[] { "srpt_geohash", "srpt_geohash", "srpt_quad", "srpt_packedquad", "srptgeom" };
String[] checkFormats = new String[] { "GeoJSON", "WKT", "POLY" };
for (String field : check) {
// Add a document with the given field
assertU(adoc("id", "test", field, wkt));
assertU(commit());
for (String fmt : checkFormats) {
String json = h.query(req("q", "id:test", "wt", "json", "indent", "true", "fl", "xxx:[geo f=" + field + " w=" + fmt + "]"));
Map<String, Object> doc = readFirstDoc(json);
Object v = doc.get("xxx");
String expect = fmts.getWriter(fmt).toString(shape);
if (!(v instanceof String)) {
v = normalizeMapToJSON(v.toString());
expect = normalizeMapToJSON(expect);
}
assertEquals("Bad result: " + field + "/" + fmt, expect, v.toString());
}
}
}
use of org.locationtech.spatial4j.io.SupportedFormats in project lucene-solr by apache.
the class GeoJSONWriter method write.
@Override
public void write(Writer writer, SolrQueryRequest req, SolrQueryResponse rsp) throws IOException {
String geofield = req.getParams().get(FIELD, null);
if (geofield == null || geofield.length() == 0) {
throw new SolrException(ErrorCode.BAD_REQUEST, "GeoJSON. Missing parameter: '" + FIELD + "'");
}
SchemaField sf = req.getSchema().getFieldOrNull(geofield);
if (sf == null) {
throw new SolrException(ErrorCode.BAD_REQUEST, "GeoJSON. Unknown field: '" + FIELD + "'=" + geofield);
}
SupportedFormats formats = null;
if (sf.getType() instanceof AbstractSpatialFieldType) {
SpatialContext ctx = ((AbstractSpatialFieldType) sf.getType()).getSpatialContext();
formats = ctx.getFormats();
}
JSONWriter w = new GeoJSONWriter(writer, req, rsp, geofield, formats);
try {
w.writeResponse();
} finally {
w.close();
}
}
use of org.locationtech.spatial4j.io.SupportedFormats in project lucene-solr by apache.
the class AbstractSpatialFieldType method init.
@Override
protected void init(IndexSchema schema, Map<String, String> args) {
super.init(schema, args);
if (ctx == null) {
// subclass can set this directly
final String CTX_PARAM = "spatialContextFactory";
final String OLD_SPATIAL4J_PREFIX = "com.spatial4j.core";
final String NEW_SPATIAL4J_PREFIX = "org.locationtech.spatial4j";
for (Map.Entry<String, String> argEntry : args.entrySet()) {
// "JTS" is a convenience alias
if (argEntry.getKey().equals(CTX_PARAM) && argEntry.getValue().equals("JTS")) {
argEntry.setValue("org.locationtech.spatial4j.context.jts.JtsSpatialContextFactory");
continue;
}
// Warn about using old Spatial4j class names
if (argEntry.getValue().contains(OLD_SPATIAL4J_PREFIX)) {
log.warn("Replace '" + OLD_SPATIAL4J_PREFIX + "' with '" + NEW_SPATIAL4J_PREFIX + "' in your schema.");
argEntry.setValue(argEntry.getValue().replace(OLD_SPATIAL4J_PREFIX, NEW_SPATIAL4J_PREFIX));
}
}
//Solr expects us to remove the parameters we've used.
MapListener<String, String> argsWrap = new MapListener<>(args);
ctx = SpatialContextFactory.makeSpatialContext(argsWrap, schema.getResourceLoader().getClassLoader());
args.keySet().removeAll(argsWrap.getSeenKeys());
}
final String distanceUnitsStr = args.remove("distanceUnits");
if (distanceUnitsStr == null) {
this.distanceUnits = ctx.isGeo() ? DistanceUnits.KILOMETERS : DistanceUnits.DEGREES;
} else {
this.distanceUnits = parseDistanceUnits(distanceUnitsStr);
if (this.distanceUnits == null)
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Must specify distanceUnits as one of " + DistanceUnits.getSupportedUnits() + " on field types with class " + getClass().getSimpleName());
}
final SupportedFormats fmts = ctx.getFormats();
String format = args.remove(FORMAT);
if (format == null) {
format = "WKT";
}
shapeWriter = fmts.getWriter(format);
shapeReader = fmts.getReader(format);
if (shapeWriter == null) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown Shape Format: " + format);
}
if (shapeReader == null) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown Shape Format: " + format);
}
argsParser = newSpatialArgsParser();
}
Aggregations